dropecho.macros

A set of utility macros for haxe.
https://github.com/dropecho/macros.hx

To install, run:

haxelib install dropecho.macros 1.0.0 

See using Haxelib in Haxelib documentation for more information.

README.md

dropecho.macros

A small Haxe library of compile-time build macros that generate constructors and fields onto a class at build time. Everything runs at compile time, so the library ships no runtime code of its own — only the code it generates.

Install

haxelib install dropecho.macros

Usage

Attach a macro to a class with @:build.

Constructor.fromFields

Generate a constructor from the class's variable fields. Each field becomes a constructor argument that is assigned to this.

@:build(dropecho.macros.Constructor.fromFields(true))
class Point {
	public var x:Int;
	public var y:Int;
}

new Point(1, 2); // x = 1, y = 2

Constructor.fromArgs

Fill in an empty constructor's body and create matching fields from its arguments.

@:build(dropecho.macros.Constructor.fromArgs(true))
class Point {
	public function new(x:Int, y:Int);
}

Constructor.fromTypeDef

Build a class (and a static build function) from a single configuration-object argument. The config's fields are copied onto the class and assigned in the constructor.

class Config {
	public var x:Int;
	public var y:Int;
}

@:build(dropecho.macros.Constructor.fromTypeDef(true))
class Point {
	public function new(config:Config);
}

The boolean argument on each macro controls whether the generated fields are public.

MakeOptional.OptionalType.optional

Copy another type's variable fields onto a class as public, optional (Null<T>) fields.

class Point {
	var x:Int;
	var y:Int;

	public function new() {}
}

@:build(dropecho.macros.MakeOptional.OptionalType.optional(Point))
class Config {
	public function new() {}
}

var c = new Config();
c.x = 1; // x and y copied from Point as Null<Int>

MathMacros.pow

Unroll an integer power into repeated multiplication at compile time (no runtime Math.pow). Compound arguments are bound to a temporary so they evaluate once.

import dropecho.macros.MathMacros;

MathMacros.pow(t, 3);     // => t * t * t
MathMacros.pow(1 - t, 2); // => { final b = 1 - t; b * b; }

Development

npm test   # run the utest suite (via dropecho.testing)

Tests are auto-discovered: any test/**/*Tests.hx class extending utest.Test is picked up by dropecho.testing — there is no hand-written test main.

License

MIT

Contributors
vantreeseba
Version
1.0.0
Published
4 months ago
Dependencies
License
MIT

All libraries are free

Every month, more than a 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

⌂