paldepind / union-type

A small JavaScript library for defining and using union types.
MIT License
478 stars 28 forks source link

Generic types #11

Closed yelouafi closed 9 years ago

yelouafi commented 9 years ago

actually

var NumList = Type({Nil: [], Cons: [Number, List]});
var StringList = Type({Nil: [], Cons: [String, List]});

with generics

var List = Type( T => {Nil: [], Cons: [T, List(T)]});

NumList = List(Number)
list = NumList.Cons( 1, NumList.Nil() )

One can actually do that himself by

var GType = FT => T => Type( FT(T) );
List = GType( T => {...})

Edit: fixed code above

but i think it's nice to have in the lib

yelouafi commented 9 years ago

Oops! this wont work

var List = Type( T => {Nil: [], Cons: [T, List(T)]});

because w'd have someway to pass the type param to the nested List, but at this point List is undefined, the GType method actually yields to in infinite recursion