Accessing underlying "this" of abstract type in a macro function.

81 views
Skip to first unread message

Dan Korostelev

unread,
Sep 16, 2013, 5:54:52 PM9/16/13
to haxe...@googlegroups.com
Hey, haxers!

I was playing with abstracts and was trying to implement signal abstract over an array of functions. What I am trying to achieve is strictly typed dispatch function (whose signature is checked against signal listener in compile-time) that is generated into a simple array iteration with function calls.

Although, I've managed to do it pretty well here: https://gist.github.com/nadako/6586991, there's still a thing that bugs me: I needed to add `length` and array access fields to my abstract that simply proxy to the underlying array, because I din't find out how to gain access to the underlying `this` in a macro function within an abstract.

So, the question is: is it even possible and what's the correct syntax?

Thanks in advance!

Dan Korostelev

unread,
Sep 18, 2013, 2:35:54 PM9/18/13
to haxe...@googlegroups.com
Anyone?

вторник, 17 сентября 2013 г., 1:54:52 UTC+4 пользователь Dan Korostelev написал:

Juraj Kirchheim

unread,
Sep 18, 2013, 3:03:27 PM9/18/13
to haxe...@googlegroups.com
This will happen to work: https://gist.github.com/back2dos/6613772

Things to note:

1. If you generate local variables, try to make sure they don't
conflict with other expressions (hence the "__").
2. Cache the target (ethis) in a variable. Imagine you were to write
`expensiveQuery().signal.dispatch(1)` then the generated code will
execute `expensiveQuery()` with every iteration of the loop.
3. The need to write `static` at that point is most likely a bug in
the implementation of abstracts. Simn has committed some stuff
regarding using and macros, which may have fixed that as well. You
might want to to try the github version. If the problem persists, it'd
would be great if you could file an issue.

Regards,
Juraj
> --
> 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/groups/opt_out.

Dan Korostelev

unread,
Sep 18, 2013, 4:02:15 PM9/18/13
to haxe...@googlegroups.com
The problem here is that that "ethis" argument is of Signal abstract type and I wonder if it's possible to generate code that works with underlying type, so I can remove unnecessary length and array access proxy methods from abstract.

среда, 18 сентября 2013 г., 23:03:27 UTC+4 пользователь back2dos написал:

Juraj Kirchheim

unread,
Sep 19, 2013, 5:14:30 AM9/19/13
to haxe...@googlegroups.com
I've updated the gist to do that. Not super pretty, but it's pretty
much the best way to go without exposing too much to client code.

Please note that if any of the expressions in `args` are not
referentially transparent, you will get the side effect multiple
times. So you probably should store them in local variables also.

Regards,
Juraj

Dan Korostelev

unread,
Sep 19, 2013, 10:09:02 AM9/19/13
to haxe...@googlegroups.com
Juraj, thanks for this. I think it's probably easier to generate an inline function for the abstract via build macros and let haxe handle local variable creation and stuff. Hope that works :)

четверг, 19 сентября 2013 г., 13:14:30 UTC+4 пользователь back2dos написал:
Reply all
Reply to author
Forward
0 new messages