mirror of
https://github.com/hanzoai/yaegi.git
synced 2026-10-01 00:49:47 +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.
23 lines
267 B
Go
23 lines
267 B
Go
package main
|
|
|
|
type Number interface {
|
|
int | int64 | ~float64
|
|
}
|
|
|
|
func Sum[T Number](numbers []T) T {
|
|
var total T
|
|
for _, x := range numbers {
|
|
total += x
|
|
}
|
|
return total
|
|
}
|
|
|
|
func main() {
|
|
xs := []int{3, 5, 10}
|
|
total := Sum(xs)
|
|
println(total)
|
|
}
|
|
|
|
// Output:
|
|
// 18
|