Using inline vars in objects as keys

I have and object with a series of value pairs:

var pairsOBJ = {

I would rather have:

public static inline var NAME:String = “name”;

var pairsOBJ = {

Problem is when I read the field, the field name is NAME rather than “name”. Is there a way to make the inline var string show up instead?

I don’t believe this is something that you can do without a macro. What is it that you are looking to do? If you just want to be able to access object fields at runtime, use the reflection API.

Looks like you’re looking for haxe.DynamicAccess

what i got from your question :

try this :

class Test {
    static function main() {
        trace("Haxe is great!");
        var pairsOBJ = { "NAME":"value" };
        var DATA:DataModal;
        DATA = pairsOBJ;

typedef DataModal=
  	var NAME:String;

You may want to use a Map instead of an object here.

var pairs = [
  NAME => "0"

If you really need it to be an object, then it’ll only work with a macro.

1 Like

I know it’s outdated but found this looking for similiar problem.
Going JS way (and Dan’s suggestion) it could be sth like:

    public static inline function NAME() { return "name"; }

    static function main() {
        var pairsOBJ:haxe.DynamicAccess<Dynamic> = {};
        pairsOBJ.set(NAME(), "value");
	trace(pairsOBJ.exists("name")); // true        

Unfortunately it seems you can’t use (even static/derived/calculated) expression neither inlined function result directly as field identifier during object declaration [w/o macros] - it could be nice if possible.