Anonymous functions as getter/setter methods?

239 views
Skip to first unread message

Cambiata

unread,
Jul 4, 2012, 10:35:04 AM7/4/12
to haxe...@googlegroups.com
I would have guessed that it would be possible to use anonymous functions as getter/setter methods. Something like this:

public var name( function() { return this._user.name; }, null): String

But it's not possible, is it? Or am I missing something?

/ Jonas

Hugh

unread,
Jul 5, 2012, 12:47:29 AM7/5/12
to haxe...@googlegroups.com
This is not possible.  But I hope it will be one day :)

Hugh

Max

unread,
Jul 5, 2012, 4:34:25 AM7/5/12
to haxe...@googlegroups.com
You can't do it like that,  but you can do it like this

public dynamic function get_name():String { return this._user.name; };
public var name(get_name, null): String

I used default getter get_name to show you that you can use the same function without changing code if you decide to use dynamic getter like this

public var name(dynamic, null): String

Furthermore, since get_name is now dynamic, you can now dynamically assign anonymous function(s) like

get_name = function() { return ""; }

everywhere and anytime in your code. This gives you playground for funny things like interface simulation.

Juraj Kirchheim

unread,
Jul 5, 2012, 12:24:09 PM7/5/12
to haxe...@googlegroups.com
Why do you want that? What does this allow you, that the current way
(or the one planned for Haxe 3) doesn't?

Regards,
Juraj

Cambiata

unread,
Jul 5, 2012, 3:56:45 PM7/5/12
to haxe...@googlegroups.com
Thank you, guys!
Just wondered if setters/getters were dynamic function handlers, just like eventhandlers etc. Now I know that they aren't!

@Juraj:
It wouldn't allow anything at all, except for the normal closure stuff...
Reply all
Reply to author
Forward
0 new messages