Macros
Overview of Haxe macros snippets and tutorials.
Mar 24, 2016 Macros / Add git commit-hash in build
This example executes a process on the system, compile-time. This allows to run a git command git rev-parse HEAD and use its result as the value.
Mar 24, 2016 Macros / Add parameters as fields
This macro function automatically assigns parameters of method to local variables.
Mar 23, 2016 Macros / Assert macro that shows sub-expression values
Sometimes failed assertion checks make it difficult to tell what went wrong. For debugging the programmer not only wants to know that a check failed, but also why it failed. This macro outputs the values of all sub-expressions.
Jan 03, 2017 Macros / Generating Arrays with values
Sometimes it is useful make your arrays compile-time, for example to embed data from files, to pre-calculate heavy calculations, generating lookup tables and other similar things. With macros this is perfectly doable but requires some basic knowledge of expression building. In this article you will find out how to build arrays and return them as expressions in a macro function.
Apr 03, 2016 Macros / Add a map
This snippet demonstrates how to add a map field to a type.
Mar 22, 2016 Macros / Add property with getter
Virtually adds this property to a class: public var myVar(get, null):Float; private inline function get_myVar():Float { return 1.5; }
Mar 22, 2016 Macros / Add a static field
Virtually adds this static variable to a class: public inline static var STATIC_VAR:Float = 1.5;
Mar 22, 2016 Macros / Create value-objects
This example generates a constructor-function for each field of a class to easily create value object classes.
Apr 19, 2016 Macros / Combine two or more structures
Haxe makes it easy to define extensions for a typedef, but there is no easy way to combine the values of two or more structures to one, like {a:2} and {b:foo} to {a:2,b:foo}. The following macro does this for you.
Mar 26, 2016 Macros / Code completion from URL
This example will load an URL, scrape all id's from the HTML page and use them for auto-completion.
Mar 25, 2016 Macros / Get all values of an @:enum abstract
The following macro function returns an array of all possible values of a given @:enum abstract type. Since it's not possible in runtime (because abstracts doesn't exist there), we need to use a macro for that.
Mar 29, 2016 Macros / Extract values from known enum instances
Sometimes we have an instance of enum that is of known constructor (or we only accept that constructor) and we want to extract values from that instance. Normally, to do this, we have to use pattern matching (switch), however it's quite verbose, so we can instead use this macro static extension method that will generate switch for us.
Dec 14, 2016 Macros / Extract values with pattern matching
Mostly useful to extract enum values from known enum instances. Allows to extract multiple variables at once. Takes most of expressions that are possible to use in switch expression.
Mar 26, 2016 Macros / Generate dispatch code
Automatically generate dispatch functions as:
Jun 16, 2016 Macros / Add build time using macro
This example shows:
Apr 25, 2016 Macros / Working with compiler flags
This snippet demonstrates how to get, set, check for, and list compiler flags.
Jul 04, 2017 Macros / Include a file next to a Haxe module file
This example lets you take a file next to the current module .hx file and include its file content. That can be very useful if you want to separate (for example) view templates, shader sources or other multiline texts from the Haxe source. The articles demonstrates how to do it with an expression macro but also with a build macro.
Apr 27, 2021 Macros / Strictly Typed JSON
It's possible read JSON files at compile time into strictly typed objects in Haxe.
Jul 01, 2019 Macros / Threading macro like Clojure and pipe operator
Introduction
Mar 22, 2016 Macros / Validates a .JSON file compile-time
Json is quite a strict format. Ensure all comma's and quotes are correct using a macro function, which is executed while compiling.