Giter VIP home page Giter VIP logo

lazy-props's Introduction

Lazy Props

Haxe lazy (less typing) property definition macro

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

In a nutshell

Lazy Props declares and assigns class properties automatically based on constructor: less_glue

Installation

Install from haxelib:

haxelib install lazyprops

Or point haxelib at the latest version in git:

haxelib git lazyprops https://github.com/jcward/LazyProps master src

Usage

After installing from haxelib, add -lib lazyprops to your build.hxml file -- (library syntax varies for OpenFL, NME, Snow, Kha, etc.)

Any classes to be parsed by Lazy Props must have the @:build(LazyProps.build()) metadata before the class declaration. Then, all Lazy Props metadata tags go on the class constructor. See the example.

Troubleshooting

I'm working on better error messages. In the meantime:

  • Ensure the @:build(LazyProps.build()) tag is present on the class
  • Check the Lazy Props tag names are spelled properly, including the @:, and without a trailing semi-colon, e.g. @:propPrivate('*')
  • Verify the validity of the syntax (no stray spaces, commas, etc)
  • Verify the property names in the tags match the variable names in the constructor
  • Ensure the property isn't already defined (in the class or super classes)

Supported metadata tags:

@: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 private readOnly, getOnly, writeOnly, and setOnly are enforced with the never access pattern (e.g. private name(default,never)) which means that the vairable cannot be written or read, even from within the class itself. The constructor assignment for set-never variables 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 in type-checked code.

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

lazy-props's People

Contributors

jcward avatar

Watchers

Weslyn Whitehead avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.