Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravinggenius/0ad40df0ab000d2823a5 to your computer and use it in GitHub Desktop.
Save ravinggenius/0ad40df0ab000d2823a5 to your computer and use it in GitHub Desktop.
Type restrictions, multi-types, generics

Types And Signatures

All objects have a single .type. Lambdas also have a list of .signatures, each unique, one for each overload. Each signature is a type.

a = 42
b = :rip
c = -> (a) { a + a }
d = -> (a<System.Integer>, b<System.Integer> = 0) { a + b }

a.type #=> System.Integer
b.type #=> System.String
c.type #=> System.Lambda
d.type #=> System.Lambda

c.signatures #=> [ System.Lambda<System.Object> ]

d.signatures #=> [ System.Lambda<System.Integer>, System.Lambda<System.Integer, System.Integer> ]

Questions

  1. If a lambda has only one signature, should its type match that signature, or should the type of every lambda always be the generic System.Lambda?
# no type annotations
fold0 = -> (folder, initial, list) {
if (items.length == 0) {
initial
} else {
fold(folder, folder(initial, items.head), items.tail)
}
}
# simple parameter type annotations (type restrictions)
# restricts the calling code from passing eg fold1('hello', 42, [])
fold1 = -> (folder<System.Lambda>, initial, list<System.List>) {
# same as above
}
# generic type annotations
# T and TT are unspecified types
# this takes type restriction to the next level by cloning the overload with the appropriate signatures resolved, each time fold2 is called (with appropriately aligned calling signatures)
fold2 = -> (folder<System.Lambda<T, TT>>, initial<T>, list<System.List<TT>>) {
# same as above
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment