getter, setter, reflection and metadata

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

getter, setter, reflection and metadata

clemos
Hi list,

Now that we have the Metadata feature, wouldn't it be nice to "merge"
the getter / setter feature with it.

Instead of writing this:
public var test ( getTest , setTest ) : String;

One would write:
@:get( getTest )
@:set( setTest )
public var test : String

The main goal would be to solve/workaround this kind of issue:
http://code.google.com/p/haxe/issues/detail?id=468

Maybe this doesn't require a change in the way getters and setters are
expressed, but at least it would be nice if the compiler could fill
the appropriate meta when dealing with such properties.
It's really a pity not to be able to use getters and setters, for
example when using hscript/templates.

As mentionned in the issue's discussion, Reflect.getProperty /
Reflect.setProperty could be implemented this way or something:
public function getProperty( o : Dynamic , field : String ){
    var meta = haxe.rtti.Meta.getFields( Type.getClass( o ) );
    var f = meta.field( field );
    if( f != null && f.get != null ) return callMethod( o , f.get , [] );
    else return field( o , field );
}
public function setProperty( o : Dynamic , field : String , value : Dynamic ){
    var meta = haxe.rtti.Meta.getFields( Type.getClass( o ) );
    var f = meta.field( field );
    if( f != null && f.set != null ) return callMethod( o , f.set , [value] );
    else return setField( o , field , value );
}

What do you think?

Cheers,
Clément

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: getter, setter, reflection and metadata

Nicolas Cannasse
Le 11/09/2011 12:53, clemos a écrit :
> Hi list,
>
> Now that we have the Metadata feature, wouldn't it be nice to "merge"
> the getter / setter feature with it.
[...]
> What do you think?

I'm planning to add support for it, but in a more compact way than medatada.

If you want to play with it you can still have a macro add all these
metadata at compilation-end (with Context.onGenerate) and implement your
own getProperty/setProperty versions based on this.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: getter, setter, reflection and metadata

clemos
Hi Nicolas,

Thanks for the suggestion.
In case someone is interrested, I've implemented it in erazor (with a
compiler flag) :
https://github.com/clemos/erazor/commit/57a97c0bcef5a0ded67822d6ec6cae880d7b2742

Cheers,
Clément

On Sun, Sep 11, 2011 at 4:17 PM, Nicolas Cannasse
<[hidden email]> wrote:

> Le 11/09/2011 12:53, clemos a écrit :
>>
>> Hi list,
>>
>> Now that we have the Metadata feature, wouldn't it be nice to "merge"
>> the getter / setter feature with it.
>
> [...]
>>
>> What do you think?
>
> I'm planning to add support for it, but in a more compact way than medatada.
>
> If you want to play with it you can still have a macro add all these
> metadata at compilation-end (with Context.onGenerate) and implement your own
> getProperty/setProperty versions based on this.
>
> Best,
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org