Macro API - Detect Properties

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

Macro API - Detect Properties

danielku15
Hi everybody.

I am currently writing a custom code generator but I encountered a problem using properties.

Properties and normal vars are both FieldKind.FVar values. Take this example:

public var MyVar:Int;
public var MyProperty(default,default):Int;

If I access those items both vars are FVar(VarAccess.AccNormal, VarAccess.AccNormal) instances. Is there a way to detect whether items are declared as a property or not?

I would recommend a VarKind like a MethodKind exists for FieldKind.FMethod:

enum FieldKind {
    FVar( k : VarKind, read : VarAccess, write : VarAccess );
    FMethod( K : MethodKind );
}

enum VarKind {
    VarNormal;
    VarProperty;
}

It seems the compiler simply transforms the access during usage according to read/write. The var stays a simple var, without any property-data.

Greetings
Daniel
Reply | Threaded
Open this post in threaded view
|

Re: Macro API - Detect Properties

Heinz Hölzer-2
I think they are equivalent. Writing (default, default) doesn't add any
new meaning to a variable, right?

Am 06.11.2011 00:54, schrieb danielku15:

> Hi everybody.
>
> I am currently writing a custom code generator but I encountered a problem
> using properties.
>
> Properties and normal vars are both FieldKind.FVar values. Take this
> example:
>
> public var MyVar:Int;
> public var MyProperty(default,default):Int;
>
> If I access those items both vars are FVar(VarAccess.AccNormal,
> VarAccess.AccNormal) instances. Is there a way to detect whether items are
> declared as a property or not?
>
> I would recommend a VarKind like a MethodKind exists for FieldKind.FMethod:
>
> enum FieldKind {
>      FVar( k : VarKind, read : VarAccess, write : VarAccess );
>      FMethod( K : MethodKind );
> }
>
> enum VarKind {
>      VarNormal;
>      VarProperty;
> }
>
> It seems the compiler simply transforms the access during usage according to
> read/write. The var stays a simple var, without any property-data.
>
> Greetings
> Daniel
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Macro-API-Detect-Properties-tp6966887p6966887.html
> Sent from the Haxe mailing list archive at Nabble.com.
>


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

Re: Macro API - Detect Properties

danielku15
Yes, in means of haXe they are the same. But I'm working on a C# code generator based on macros and it simply works great yet. The problem: all elements are public members which is bad design. I'd like to generate members as properties if they are declared as properties in haXe.
Reply | Threaded
Open this post in threaded view
|

Re: Macro API - Detect Properties

Juraj Kirchheim
Would it be good enough to generate properties for all public
variables in haXe, that aren't FVar(AccNormal, AccNormal)?

Please also note, that haXe doesn't usually generate accessors for
it's target platforms, but rather replaces field access with method
calls at compile time, which creates a very consistent behavior across
platforms.


On Sun, Nov 6, 2011 at 4:27 PM, danielku15 <[hidden email]> wrote:

> Yes, in means of haXe they are the same. But I'm working on a C# code
> generator based on macros and it simply works great yet. The problem: all
> elements are public members which is bad design. I'd like to generate
> members as properties if they are declared as properties in haXe.
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Macro-API-Detect-Properties-tp6966887p6968104.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

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

Re: Macro API - Detect Properties

danielku15
Juraj Kirchheim wrote
Would it be good enough to generate properties for all public
variables in haXe, that aren't FVar(AccNormal, AccNormal)?
That's the code I had before and this lead to some really ugly statements I want to prevent:

public static var DEFAULT_VALUES:Array<Int> = [1,2,3];

will become:

private static HxArray<int> _DEFAULT_VALUES = new HxArray<int>(1,2,3);
public static HxArray<int> DEFAULT_VALUES {
    get { return _DEFAULT_VALUES; }
    set { _DEFAULT_VALUES = value; }
}

Okay it works, but I really would prefer to create C# code that looks nearly the same as it's haXe equivalent. As my project is a library for other developers, I'd really like to see clean and usable code produced by my generators.

Juraj Kirchheim wrote
Please also note, that haXe doesn't usually generate accessors for
it's target platforms, but rather replaces field access with method
calls at compile time, which creates a very consistent behavior across
platforms.
That's what I thought it would be, but it would be great if I could provide clean properties for a generated C# library. I'll probably introduce some metadata tags to annotate properties should get generated but that's still a little bit redundant.