Skip to content

Instantly share code, notes, and snippets.

@landonf
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landonf/5d074765c7d9ffac23d7 to your computer and use it in GitHub Desktop.
Save landonf/5d074765c7d9ffac23d7 to your computer and use it in GitHub Desktop.
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