Skip to content

Instantly share code, notes, and snippets.

View nadako's full-sized avatar

Dan Korostelev nadako

View GitHub Profile
@nadako
nadako / DynamicMap.hx
Last active January 2, 2016 22:29
Abstract wrapper for Dynamic objects.
/**
* Simple wrapper for anonymous structures that are meant to be used as a
* keyed collection of objects of the same type (i.e. json object).
*
* Wraps Reflect calls with nice syntax and static typing without any
* runtime overhead.
*/
abstract DynamicMap<V>(Dynamic<V>) from Dynamic<V>
{
/**
@nadako
nadako / EnumAbstract.hx
Last active January 3, 2016 07:19
Incomplete backport of @:enum abstracts from Haxe 3.1 to Haxe 3.0
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.TypeTools;
/**
* Частичный бекпорт функциональности @:enum abstract из Haxe 3.1
*
* @:enum abstract'ы - элегантная замена набору констант с проверкой значений на этапе компиляции.
@nadako
nadako / CommandMacro.hx
Last active January 3, 2016 12:38
Check command arguments object for missing keys and bad type.
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import haxe.macro.ComplexTypeTools;
class CommandMacro
{
public static function build():Array<Field>
{
@nadako
nadako / Main.hx
Created January 19, 2014 13:29
stack-allocated pair iterator
class Main
{
static function main()
{
var a = ["a", "b", "c"];
for (p in new IndexIter(a))
{
trace(p.idx);
trace(p.val);
@nadako
nadako / Const.hx
Created February 14, 2014 11:06
@:genericBuild read-only type builder
@:genericBuild(ConstMacro.build())
class Const<T> {}
@nadako
nadako / 1 Main.hx
Created February 18, 2014 22:19
Using haxe macros as syntax-tolerant, position-aware json parser
import haxe.macro.Context;
import haxe.macro.Expr;
using haxe.macro.ExprTools;
class Main
{
inline static var QUOTED_FIELD_PREFIX = "@$__hx__";
static function main()
@nadako
nadako / ArrayRead.hx
Last active March 14, 2024 07:57
Recursive read-only type generator for JSON structures (based on @:genericBuild)
abstract ArrayRead<T>(Array<T>) from Array<T> {
@:arrayAccess inline function get(i:Int):T return this[i];
public var length(get,never):Int;
inline function get_length() return this.length;
}
@nadako
nadako / Main.hx
Created March 5, 2014 16:03
Haxe for Unity with -net-lib
class Main extends unityengine.MonoBehaviour
{
function Update()
{
unityengine.Debug.Log("Hello");
}
}
import haxe.macro.Context;
import haxe.macro.Expr;
class EnumAbstractMacro
{
public static function build():Array<Field>
{
var fields = Context.getBuildFields();
var values = [];
@nadako
nadako / Main.hx
Created May 5, 2014 12:51
Typed hasField check macro
using TypedFieldMacro;
typedef A = {?f:Int}
class Main
{
static function main()
{
var a:A = {f:1};
if (a.f.exists()) trace(a.f);