2

I want to be able to set and get a property value by its name without using the Reflect API. I'm making some sort of a tween engine, animating hundreds of objects and the call to the function Reflect.getProperty and setProperty is taking quite some CPU time.

Is there a way to bypass the Reflect API without having to inject raw javascript inside my Haxe code ? (so it can also be used on other platforms than js)

So instead of doing this:

finalValue = Reflect.getProperty(object, propertyName);

I would like to be able to do this:

finalValue = object[propertyName];

thanks.

Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68

2 Answers2

1

What about going untyped?

    // set
    untyped object[propertyName] = 15;

    // get
    var value = untyped object[propertyName];

Try it yourself: http://try.haxe.org/#5b61e

Warn Going untyped lets you omit the type system and is more error prone. If you use instances with Haxe properties (getter/setters) it might give unexpected results. That's why using Reflect is considered safer.

Mark Knol
  • 9,663
  • 3
  • 29
  • 44
1

You need to be careful when using "untyped" with properties that are getters/setters and standard properties, because the calls are slightly different. eg:

// set
untyped object["set_" + propertyName] = 15;

// get
var value = untyped object["get_" + propertyName];

Typically what we do with our tween library is do a check up front when the property is mapped to see if it is a getter/setter and store this information for runtime use.

user2630051
  • 119
  • 3