lazyprops

Haxe macro for generating property definitions and assignment automatically from the constructor.
http://github.com/jcward/lazy-props

To install, run:

haxelib install lazyprops 0.0.3 

See using Haxelib in Haxelib documentation for more information.

README.md

Lazy Props

Haxe lazy (less typing) property definition macro

Motivation described in the writeup here: jcward.com/Less+Glue+via+Haxe+Macro+Lazy+Props

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(LazyProps.build()) metadata to invoke the Lazy Props macro. See the example.

@:prop()
@:propPublic()
@:propPrivate()
@:propPublicGetSet()
@:propPrivateGetSet()
@:propPublicReadOnly()
@:propPublicWriteOnly()
@:propPublicGetOnly()
@:propPublicSetOnly()
@:propPrivateReadOnly()
@:propPrivateWriteOnly()
@:propPrivateGetOnly()
@:propPrivateSetOnly()
@:publicPrefix()
@:privatePrefix()

@: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:

@:propPublicReadOnly('name,age')
@:propPrivate('*')

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.
Contributors
jeff.ward
Version
0.0.3
Published
2 years ago
License
MIT

All libraries are free

Every month, more than thousand developers use haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on Github!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on Github