Skip to content

Latest commit

 

History

History
56 lines (37 loc) · 1.87 KB

README.md

File metadata and controls

56 lines (37 loc) · 1.87 KB

Haxe Thingies

This repo contains several useful little types and macros for Haxe that may or may not ease developer's life. See the code or unit tests to get the idea how and when to use these.

DynamicObject

This type wraps Reflect field access for dynamic structures in a Map-like interface, making it easier to work with JSON or external JavaScript objects.

NOTE: this abstract is now part of Haxe standard library (since 3.2.0), see https://github.com/HaxeFoundation/haxe/blob/development/std/haxe/DynamicAccess.hx

var d:DynamicObject<Int> = haxe.Json.parse('{"item1": 1, "item2": 2}');
for (key in d.keys())
    trace(d[key]);

is equivalent to

for (key in Reflect.fields(d))
    trace((Reflect.field(d, key) : Int));

OptionalField

This type provides explicit, but convenient syntax for working with optional and potentially missing structure fields, wrapping Reflect.hasField calls.

var house:{?tennant:OptionalField<String>} = {};
var tennant = house.tennant.or("Ghosts");

is equvalent to

var tennant = Reflect.hasField(house, "tennant") ? house.tennant : "Ghosts";

Unpack

This is a using-macro implementing CoffeeScript-like value destructuring (http://coffeescript.org/#destructuring).

var value = [{f: [1, 2, 3]}, {f: [1]}, {f: [1]}, {f: []}];
value.unpackInto({f: v1}, {f: [v2]}, _, {f: {length: v3}});

This unpacks arrays and object fields into local variables similar to pattern-matching, but with shorter syntax. In the example above, there will be 3 variables generated: v1, capturing [1, 2, 3], v2 capturing 1 and v3 capturing 0.

Yet to add