Skip to content

Instantly share code, notes, and snippets.

public static function getFirstPath (from:ClassType, to:ClassType):Option<Path>
{
function loop(from:ClassType, to:ClassType, path:Path)
{
trace(loop);
return if (ClassTypes.eq(from, to)) {
Some(path);
} else {
public static function getFirstPath (from:ClassType, to:ClassType):Option<Path>
{
function loop(from:ClassType, to:ClassType, path:Path)
{
trace(loop); // boom, loop is null in my case
function findInSuperOrInterface ()
{
function findInSuper ()
// two special types to fake type constructor ploymorphism.
@:native('Dynamic')
class Of<M,A> {}
class In {}
interface Functor<F>
{
public function map<A,B>(val:Of<F,A>, f:A->B):Of<F,B>;
}
class ArrayFunctor implements Functor<Array<In>>
@frabbit
frabbit / gist:4317936
Created December 17, 2012 12:26 — forked from anonymous/gist:4317927
Haxe Partial Application Proposal
// - Implicit and explicit PA (via partial function)
// - Short alternative for partial => part, foo.part(_)
// - Implicit as default usage (short and concise), explicit for corner cases, where implicit usage is not possible/ambigous
// - Explicit PA can handle all cases that implicit PA can handle + corner cases
// - Implicit PA is only applied if one of the arguments is an underscore
function foo(x:Array<Int>, ?a:Int, ?b:Int):Void;
// Implicit Explicit Type
abstract FunctionReturning<T>(Dynamic) {
inline function new (x:Dynamic) this = x;
@:from static inline function from0 <T>(x:Void->T):FunctionReturning<T> return new FunctionReturning(x);
@:from static inline function from1 <T>(x:Dynamic->T):FunctionReturning<T> return new FunctionReturning(x);
@:from static inline function from2 <T>(x:Dynamic->Dynamic->T):FunctionReturning<T> return new FunctionReturning(x);
@:from static inline function from3 <T>(x:Dynamic->Dynamic->Dynamic->T):FunctionReturning<T> return new FunctionReturning(x);
/* ... */
}
class Test {
abstract Choice<A,B>(Dynamic) {
inline function new (x:Dynamic) this = x;
@:from static inline function fromA <A,B>(x:A):Choice<A,B> return new Choice(x);
@:from static inline function fromB <A,B>(x:B):Choice<A,B> return new Choice(x);
}
class Test {
public static function expectStringOrInt (x:Choice<String, Int>) {
@frabbit
frabbit / TypeClassDescription.hx
Last active December 19, 2015 02:48
type class description
package ;
using TestX.Eqs;
// consider the following interfaces
interface Compare<T> {
public function compare(b:T):Int;
}
interface Eq<T> {
@frabbit
frabbit / Main.hx
Last active December 21, 2015 01:59
Builder Pattern with Phantom Types
using Main.PosBuilders;
typedef Pos = { x: Int, y:Int};
private extern class ISSET {}
private extern class UNSET {}
@:allow(Main.PosBuilders) class PosBuilder<X,Y> {
public function new (x,y) {
this.x = x;
@frabbit
frabbit / Category.hx
Last active December 31, 2015 10:49
Type Constructor Polymorphism: Implementation Specs
interface Category<M>
{
// identity value in this category
public function id <A>():OfOf<M, A, A>;
/**
* Category composition Operator.
*
**/
public function dot <A,B,C>(g:OfOf<M, B, C>, f:OfOf<M, A, B>):OfOf<M, A, C>;
interface Filterable<M, A> {
public function filter (f:A->Bool):Of<M,A>;
}
class MyArray<A> implements Filterable<MyArray<In>, A> {
var a:Array<A>;
public function new (?a:Array<A>) {
this.a = a == null ? [] else a;
}
public function filter(f:A->Bool):MyArray<A> {