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.
40 lines
647 B
Go
40 lines
647 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
// SumInts adds together the values of m.
|
|
func SumInts(m map[string]int64) int64 {
|
|
var s int64
|
|
for _, v := range m {
|
|
s += v
|
|
}
|
|
return s
|
|
}
|
|
|
|
// SumFloats adds together the values of m.
|
|
func SumFloats(m map[string]float64) float64 {
|
|
var s float64
|
|
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("Non-Generic Sums: %v and %v\n",
|
|
SumInts(ints),
|
|
SumFloats(floats))
|
|
}
|