mirror of
https://github.com/traefik/yaegi.git
synced 2026-06-01 18:37:56 +00:00
Status: * [x] parsing code with generics * [x] instantiate generics from concrete types * [x] automatic type inference * [x] support of generic recursive types * [x] support of generic methods * [x] support of generic receivers in methods * [x] support of multiple type parameters * [x] support of generic constraints * [x] tests (see _test/gen*.go) Fixes #1363.
43 lines
648 B
Go
43 lines
648 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type List[T any] struct {
|
|
head, tail *element[T]
|
|
}
|
|
|
|
// A recursive generic type.
|
|
type element[T any] struct {
|
|
next *element[T]
|
|
val T
|
|
}
|
|
|
|
func (lst *List[T]) Push(v T) {
|
|
if lst.tail == nil {
|
|
lst.head = &element[T]{val: v}
|
|
lst.tail = lst.head
|
|
} else {
|
|
lst.tail.next = &element[T]{val: v}
|
|
lst.tail = lst.tail.next
|
|
}
|
|
}
|
|
|
|
func (lst *List[T]) GetAll() []T {
|
|
var elems []T
|
|
for e := lst.head; e != nil; e = e.next {
|
|
elems = append(elems, e.val)
|
|
}
|
|
return elems
|
|
}
|
|
|
|
func main() {
|
|
lst := List[int]{}
|
|
lst.Push(10)
|
|
lst.Push(13)
|
|
lst.Push(23)
|
|
fmt.Println("list:", lst.GetAll())
|
|
}
|
|
|
|
// Output:
|
|
// list: [10 13 23]
|