Skip to content

Instantly share code, notes, and snippets.

View nadako's full-sized avatar

Dan Korostelev nadako

View GitHub Profile
@:remove
@:autoBuild(SkipSerializeMacro.build())
interface ISkipSerialize {}
@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
}
}
/**
Хеширование строки
**/
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 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>,
@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 / 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 / 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 / Main.hx
Last active November 27, 2020 16:08
Signal builder using new Rest type parameter in Haxe
class Main {
static function main() {
var signal = new Signal<Int,String>();
var conn = signal.connect(function(a, b) {
trace('Well done $a $b');
});
signal.dispatch(10, "lol");
}
}
@nadako
nadako / CheckMutating.hx
Created July 15, 2014 18:32
Static check for mutating method calls
import haxe.macro.Context;
import haxe.macro.Type;
using haxe.macro.Tools;
class CheckMutating {
static var metaToRemove:Array<MetaAccess>;
static function use() {
Context.onGenerate(function(types) {
@nadako
nadako / RuntimeCheck.hx
Created June 9, 2014 19:45
Runtime type check generator.
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
using haxe.macro.Tools;
#end
@:enum abstract MyEnum<T>(T) {}
typedef A = MyEnum<Float>;
typedef B = Array<{a:Int, ?b:Array<{?a:String}>}>;
typedef C = Dynamic<Int>;