Skip to content

Instantly share code, notes, and snippets.

View nadako's full-sized avatar

Dan Korostelev nadako

View GitHub Profile
@nadako
nadako / Main.hx
Last active February 19, 2019 02:07
Tuple builder using new Rest feature of @:genericBuild
class Main {
static function main() {
var a = getValue();
}
static function getValue():Tuple<Bool,Int> {
return new Tuple(true, 10);
}
}
@nadako
nadako / EnumComparisonMacro.hx
Created August 20, 2014 13:05
Order based comparison operators for non-integer @:enum abstracts
import haxe.macro.Context;
import haxe.macro.Expr;
class EnumComparisonMacro {
static function build() {
// get ComplexType for the abstract we're building
var t = switch(Context.getLocalType()) {
case TInst(_.get() => {kind: KAbstractImpl(_.get() => ab)}, _): TPath({pack: ab.pack, name: ab.name});
case _: throw false;
}
@nadako
nadako / Either.hx
Created August 29, 2014 15:11
Either type builder using new Rest feature
@:dce
@:genericBuild(EitherMacro.build())
class Either<Rest> {}
@nadako
nadako / Main.hx
Created October 31, 2014 14:38
Structure initialization helper macro
import StructHelper.make;
typedef A = {
a:Int,
b:String,
c:Float,
d:Bool,
e:Array<Int>,
f:{
g:Array<Bool>,
/**
Хеширование строки
**/
public static function hash(key:String):Int {
// простейший алгоритм djb2
inline function cca(s:String, i:Int):Int {
// наиболее быстрое и небезопасное получение кода символа
#if js
return (untyped s).charCodeAt(i);
@nadako
nadako / Main.hx
Created November 18, 2014 13:27
Type params syntax sugar.
class Main {
static function main() {
var event:MapChangeEvent<Map<String,Int>>;
$type(event.key); // String
$type(event.value); // Int
}
}
@:remove
@:autoBuild(SkipSerializeMacro.build())
interface ISkipSerialize {}
import haxe.Constraints.Function;
@:pythonImport("flask", "Flask")
extern class Flask {
function new(module:String);
function run():Void;
function route<T:Function>(path:String):T->T;
}
class Main {
using Tools;
class Main {
static function main() {
var a = haxe.ds.Option.Some(42);
var s = a.extract(Some(v) => {value: v});
trace(s);
}
}
@nadako
nadako / DI.hx
Created April 16, 2015 19:04
Simple macro-based dependency injection system based on factory functions
import haxe.macro.Expr;
import haxe.Constraints.Function;
#if macro
import haxe.macro.Context;
import haxe.macro.Type;
using haxe.macro.Tools;
#end
class DI {