Cook
Unified Constructors for Haxe
Usage
using cook.Core;
function main(){
final b = TestClass.cook();//By convention the type uses it's own name to put it's constructors in
$type(b);
final c = OtherTestClass.cook();
final d = b.cons(c);
$type(d);
d.OtherTestClass.one().apply()
}
class TestClass{
public function new(o:String){
}
static public inline function cook(){
return Assoc.lift({ TestClass : Cook.pure(main) });
}
static public function main(o:{ ?v:Int }){
return new TestClass('${o.v}');
}
class OtherTestClass{
public function new(o:{q: String}){
}
static public inline function cook(){
return Assoc.lift({
OtherTestClass : Cook.pure(main).cons(mainI)
});
}
static public function main(o:{ q:String }){
return new OtherTestClass(o);
}
static public function mainI(o : Int){
return new OtherTestClass({ q : '$o'});
}
}
The two main class are Assoc and Cook
Assoc
Assoc produces a well typed combination of two structural (typedef'ed or anon) types.
{ a : Int } cons {b : String } == { a : Int, b : String }
In this case we use them to grow a reference tree.
{ TypeName : ...constructors } cons { OtherTypeName : ...constructors }
Cook
Cook is a tuple of constructor functions (Partial), also connected by a cons function.
Cook is a stack, retrieves the last added constructor first.
in final ctrs = a.cons(b).cons(c), a can be reached by ctrs.two() and called ctrs.two().apply(...args)
c is reached with ctrs.zero `
Partial
Partial is a wrapper for a constructor function with one argument, a structural type of some shape. It's not defined like that becauase the typing would be complex, but the cons function on it is.