Is there any way to have a typedef that still allows dynamic/custom fields?

78 views
Skip to first unread message

Jérémy Faivre

unread,
Oct 16, 2017, 8:33:45 AM10/16/17
to Haxe
Sometimes, I would like to work with typed structures that would still allow custom fields.

There may be a set of fields that are always the same (and can be typed with a typedef etc...), but I still want to allow to add custom fields to the same object, like you would do on a Dynamic object.

Unfortunately, I didn't find any way to do this. I either have a strictly typed object that doesn't allow any additional fields, or I can just use Dynamic, but then I lose all typing information, code completion on the expected "default" fields.

I'm interested to know if anybody got similar problems and how they solve it in Haxe (without the use of plain Dynamic objects and Reflect.field/setField)

Thanks!

David Elahee

unread,
Oct 16, 2017, 8:36:14 AM10/16/17
to haxe...@googlegroups.com

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
David Elahee


Dan Korostelev

unread,
Oct 16, 2017, 8:55:01 AM10/16/17
to Haxe
One option that comes to mind is a combination of abstract + field forwarding + operator overloading, like this: https://try.haxe.org/#a006A

понедельник, 16 октября 2017 г., 15:33:45 UTC+3 пользователь Jérémy Faivre написал:

Jérémy Faivre

unread,
Oct 16, 2017, 8:56:27 AM10/16/17
to Haxe
Thanks!

Unfortunately, DynamicAccess is just a nicer wrapper around Dynamic to get some syntax sugar, but it doesn't solve the problem of losing typing information and code completion on the fields we know about in advance.

Jérémy Faivre

unread,
Oct 16, 2017, 9:03:17 AM10/16/17
to Haxe
Thanks Dan, that's indeed an interesting option! I will see if that can help in my use case :)

David Elahee

unread,
Oct 16, 2017, 9:07:02 AM10/16/17
to haxe...@googlegroups.com
Not understanding everything, Abstract will provoke a type loss at runtime _but_ I think it's a very good option in general case...

If in the end it's not what you seek, I humbly suggest a "try.haxe.org" sample so that we can iterate on that :)

Bye!

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
David Elahee


Jérémy Faivre

unread,
Oct 16, 2017, 9:36:09 AM10/16/17
to Haxe
It's ok to lose type at runtime, but that was not what I'm talking about. I was talking about allowing Dynamic access on an object that still provide code completion for typed fields.

Anyway, I think Dan's option is working :) I was able to set up a type that does what I mentioned above: https://try.haxe.org/#69D3A

a and b fields are still properly typed (and code completion works), and I can still add custom fields with array access. Seems good!
Reply all
Reply to author
Forward
0 new messages