Skip to content

Instantly share code, notes, and snippets.

@happy-bracket
Created January 9, 2020 11:46
Show Gist options
  • Save happy-bracket/c3aaa4f6c18691ac4de7364e54161105 to your computer and use it in GitHub Desktop.
Save happy-bracket/c3aaa4f6c18691ac4de7364e54161105 to your computer and use it in GitHub Desktop.
HList, but in Kotlin!
sealed class HList<T>
object HNil : HList<Nothing>()
data class HNode<T, H : HList<*>>(
val value: T,
val next: H
) : HList<H>()
infix fun <A, B> A.hcons(b: B): HNode<B, HNode<A, HNil>> =
HNode(b, HNode(this, HNil))
infix fun <A, H : HList<*>> H.hcons(a: A): HNode<A, H> =
HNode(a, this)
val hlist: HNode<Long, HNode<Int, HNode<String, HNode<Boolean, HNil>>>> =
true hcons "sss" hcons 3 hcons 4L
@caeus
Copy link

caeus commented Mar 15, 2022

Cool, but I think the T type param in HList, is not necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment