2

I need to detect if a property of an object is numeric or not ( Int or Float ) What I have is the object reference, and property name as string

Here is my implementation, compoent is the object reference, but it didn't work

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;
    }

Any one can help?

simo
  • 23,342
  • 38
  • 121
  • 218

1 Answers1

4

It's a bit simpler than what you're trying: 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) );
  }
}

See http://try.haxe.org/#6A9Bd

Jason O'Neil
  • 5,908
  • 2
  • 27
  • 26