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.
35 lines
667 B
Go
35 lines
667 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
// SumIntsOrFloats sums the values of map m. It supports both int64 and float64
|
|
// as types for map values.
|
|
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
|
|
var s V
|
|
for _, v := range m {
|
|
s += v
|
|
}
|
|
return s
|
|
}
|
|
|
|
func main() {
|
|
// Initialize a map for the integer values
|
|
ints := map[string]int64{
|
|
"first": 34,
|
|
"second": 12,
|
|
}
|
|
|
|
// Initialize a map for the float values
|
|
floats := map[string]float64{
|
|
"first": 35.98,
|
|
"second": 26.99,
|
|
}
|
|
|
|
fmt.Printf("Generic Sums: %v and %v\n",
|
|
SumIntsOrFloats[string, int64](ints),
|
|
SumIntsOrFloats[string, float64](floats))
|
|
}
|
|
|
|
// Output:
|
|
// Generic Sums: 46 and 62.97
|