Как определить, является ли свойство числовым типом в haxe?

Мне нужно определить, является ли свойство объекта числовым или нет (Int или Float). У меня есть ссылка на объект и имя свойства в виде строки

Вот моя реализация, compoent - ссылка на объект, но она не сработала

public function IsNumeric():Bool
    {
        if (Std.is(Type.typeof(Reflect.getProperty(compoent, propertyName)), Int)) return true;
        if (Std.is(Type.typeof(Reflect.getProperty(compoent, propertyName)), Float)) return true;
        return false;
    }

Кто-нибудь может помочь?


person simo    schedule 19.12.2013    source источник


Ответы (1)


Это немного проще, чем вы пытаетесь: Std.is( value, type )

class Test {
  static function main(){
    js.Lib.alert( Std.is("string", Int) );
    js.Lib.alert( Std.is(0, Int) );
    js.Lib.alert( Std.is(0.3, Int) );
    js.Lib.alert( Std.is("string", Float) );
    js.Lib.alert( Std.is(0, Float) );
    js.Lib.alert( Std.is(0.3, Float) );
  }
}

См. http://try.haxe.org/#6A9Bd.

person Jason O'Neil    schedule 20.12.2013