Last active
August 29, 2015 14:02
-
-
Save landonf/5d074765c7d9ffac23d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main () { | |
let typedHeterogeneousList = true =+= "Hello" =+= 1 =+= HNil() | |
let first: Bool = typedHeterogeneousList.head() | |
let second: String = typedHeterogeneousList.tail().head() | |
let third: Int = typedHeterogeneousList.tail().tail().head() | |
} | |
protocol HList { | |
typealias Head | |
typealias Tail | |
} | |
struct HNil : HList { | |
typealias Head = Void | |
typealias Tail = Void | |
} | |
class HCons<H, T : HList> : HList { | |
typealias Head = H | |
typealias Tail = T | |
// Use func indirection to hack around: | |
// LLVM ERROR: unimplemented IRGen feature! non-fixed class layout | |
let _head: () -> Head | |
func head () -> Head { return _head() } | |
let _tail: () -> Tail | |
func tail () -> Tail { return _tail() } | |
init (_ head: H, _ tail: T) { | |
self._head = {head} | |
self._tail = {tail} | |
} | |
} | |
operator infix =+= { | |
associativity right | |
} | |
func =+=<H, L : HList> (lhs: H, rhs: L) -> HCons<H, L> { | |
return HCons(lhs, rhs) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment