Skip to content

Instantly share code, notes, and snippets.

@maiermic
Last active April 5, 2016 17:12
Show Gist options
  • Save maiermic/027c3d0fa9dd0cd9d08ccf41746832d0 to your computer and use it in GitHub Desktop.
Save maiermic/027c3d0fa9dd0cd9d08ccf41746832d0 to your computer and use it in GitHub Desktop.
rewrite Ceylon's build-in type Tuple
class MyTuple<out Element, out First, out Rest = []>
(first, rest)
extends Object()
satisfies List<Element>
given First satisfies Element
given Rest satisfies List<Element>
{
"The first element of this tuple. (The head of the
linked list.)"
shared actual
First first;
"A tuple with the elements of this tuple, except for the
first element. (The tail of the linked list.)"
shared actual
Rest rest;
shared actual native
Integer lastIndex => rest.size;
shared actual native
Integer size => 1 + rest.size;
shared actual
Element? getFromFirst(Integer index)
=> switch (index <=> 0)
case (smaller) null
case (equal) first
case (larger) rest.getFromFirst(index - 1);
}
MyTuple<Integer, Integer, Empty> myTuple = MyTuple(1, empty);
MyTuple<String|Integer, String, MyTuple<Integer,Integer,Empty>> myTuple2 =
MyTuple("text", MyTuple(1, empty));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment