Created
January 2, 2021 03:25
-
-
Save tdakkota/7164009e272fc0e88bc071916950adde to your computer and use it in GitHub Desktop.
HList in go2go. https://go2goplay.golang.org/p/7txHLqANAiu
This file contains 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
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
type HList interface { | |
hlist() | |
} | |
func (n Nil) hlist() {} | |
func (c Cons[H, T]) hlist() {} | |
type Nil struct{} | |
type Cons[H any, T HList] struct { | |
Head H | |
Tail T | |
} | |
func Create[H any](h H) Cons[H, Nil] { | |
return Cons[H, Nil]{ | |
Head: h, | |
Tail: Nil{}, | |
} | |
} | |
func Prepend[H, T any, L HList](l Cons[T, L], h H) Cons[H, Cons[T, L]] { | |
return Cons[H, Cons[T, L]]{ | |
Head: h, | |
Tail: l, | |
} | |
} | |
func PopFront[H, T any, L HList](l Cons[H, Cons[T, L]]) (H, Cons[T, L]) { | |
return l.Head, l.Tail | |
} | |
func main() { | |
a := Create(1) | |
b := Prepend(a, "abc") | |
c := Prepend(b, true) | |
d := Prepend(c, new(http.Client)) | |
fmt.Println(d) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment