lazyprops 0.0.3

Haxe macro for generating property definitions and assignment automatically from the constructor.

Released 2016-03-19.

To install, run:

haxelib install lazyprops 0.0.3

See using Haxelib in Haxelib documentation for more information.

Current version0.0.3
StatisticsInstalled 25 times

Lazy Props

Haxe lazy (less typing) property definition macro

Motivation described in the writeup here:

Declares and assigns class properties automatically based on constructor: less_glue

Supported metadata tags:

All Lazy Props metadata tags go on the class constructor. The class itself must be prepended with the @:build( metadata to invoke the Lazy Props macro. See the example.


@:prop syntax

The syntax for @:prop('') is similar to the Haxe property declaraction syntax, except without the type definition, and all the string constant names can be abbreviated. Examples include:

@:prop('public name(get,set)')
@:prop('pub name(g,s)')         // public get set
@:prop('pri name(def,n)')       // private default null
@:prop('u name(d,v)')           // public default never
@:prop('pu name(n,ne)')         // public null never

@:prop* syntax

The syntax for the other property metatags (e.g. @:propPrivate(''), @:propPublic(''), etc) is typically a comma separated list with no spaces. There is also a * which catches all remaining constructor properties that haven't yet been defined. The * supports an ignore list (to avoid redeclarations in classes where a property is defined by a super class.)

@:propPrivate('*')              // all properties in the constructor become private vars
@:propPrivate('*-name,age')     // all properties (except `name` and `age`)

In this example, two properties are declared public read-only, while the rest are declared private:


Note that privateReadOnly and privateGetOnly uses the never access pattern (e.g. private name(default,never)) which means that the vairable cannot be set, even from within the class itself. The constructor assignment inserted uses a dynamic assignment (e.g. Reflect.setField(this, "name", name); to avoid a compiler error, thus assigning the constructed value, but never allowing it to be changed.

@:*prefix syntax

The @:privatePrefix(''), @:publicPrefix('') metadata tags allow your class members to use a prefixed naming convention compared to your constructor parameters. For example, with a private prefix of @:privatePrefix('_'), the constructor arg width:Float becomes the private member variable _width:Float.

  • The default prefix for both public and private is an empty string (no prefix.)
  • The metatags are defined in order, so these tags should go at the top of the list.