code-cookbook icon indicating copy to clipboard operation
code-cookbook copied to clipboard

macros article: accessing structure properties

Open cancerberoSgx opened this issue 6 years ago • 3 comments

Similar to "Combine two or more structures" adds methods to list properties and read/write. I though interesting to have another example on this since that was the only one mixing macros and static extensions and it's useful.

cancerberoSgx avatar Jul 11 '19 16:07 cancerberoSgx

The props function can be improved (no need to do three passes). But more importantly, the get and set functions are useless, they are simply identical to accessing the field of a structure (as the compiled output shows). You cannot provide a dynamic field name to the functions, it has to be a constant, since the argument is a String. There is no Reflection fallback, and reflection is necessary to access fields dynamically.

So a.get("foo") is just a.foo

Aurel300 avatar Jul 11 '19 16:07 Aurel300

@Aurel300 aja, thanks.Maybe I was too quickly to create this PR. My objective is to list all properties, iterate and then get/set dynamically, but maybe is not possible. COuld you let me check/test and if I conclude is wrong just close this PR since it doesn't add any value to the existing article ? THanks

cancerberoSgx avatar Jul 11 '19 19:07 cancerberoSgx

It sounds like you're looking for haxe.DynamicAccess (convenience wrapper for reflection).

Gama11 avatar Jul 11 '19 20:07 Gama11

Closing due to lack of follow-up.

Simn avatar Jan 23 '24 20:01 Simn