holi_macros

Haxe macro helper functions


Keywords
macro, macros
License
BSD-3-Clause
Install
haxelib install holi_macros 1.0.0

Documentation

HOLI Macros

Installation

# Install from haxelib
haxelib install holi_macros

# Install latest version from github
haxelib git holi_macros <github url>

Description

Macro helper utilities for some of your macro needs. The goal is to keep updating this project with new solutions.

Usage

MacroHelper.autoOptionalArg

What it does: All public attributes of a class as optional constructor arguments

Parameters: optional regex string of variable names to exclude

How to use:

@:build(holi_macros.MacroHelper.autoOptionalArg('^(?!_|name).+'))
class Potato
{
    public var name:String;
    public var taste:String;
    public var color:Int;
    public var size:Float;
    public var _value:Int;

    public function new():Void
    {
        name = '$taste and $color';
    }
}

The constructor will look like this:

new Potato(?color:Int, ?size:Float, ?taste:String);

Note the optional regex '^(?!_|name).+' which excludes variable names starting with an underscore or variable names matching name.

MacroHelper.ClassBuilder

What it does: Helps making build macros

How to use:

// a build macro that does nothing
macro static function myBuildMacro1():Array<Field>
{
    var builder = new holi_macros.MacroHelper.ClassBuilder(
        Context.getLocalType(),
        Context.toComplexType(Context.getLocalType()),
        Context.getBuildFields()
    );

    return builder.toFields();
}

// a build macro adds a static variable
macro static function myBuildMacro2():Array<Field>
{
    var builder = new holi_macros.MacroHelper.ClassBuilder(
        Context.getLocalType(),
        Context.toComplexType(Context.getLocalType()),
        Context.getBuildFields()
    );

    var myvariable = new holi_macros.MacroHelper.MVariable(
        'potato',
        Context.currentPos(),
        [APublic, AStatic],
        macro:String
    );

    builder.variables.set('potato', myvariable);

    return builder.toFields();
}

// add an first argument and expression to the constructor
macro static function myBuildMacro3():Array<Field>
{
    var builder = new holi_macros.MacroHelper.ClassBuilder(
        Context.getLocalType(),
        Context.toComplexType(Context.getLocalType()),
        Context.getBuildFields()
    );

    var constructor = getOrCreateConstructor(builder, Context.currentPos());
    constructor.addArgFirst({name:"tomato", type: macro:String, opt:true});
    constructor.addExpr(macro {trace(tomato);});
    builder.functions.set('new', constructor);

    return builder.toFields();
}