Source file
test/typeparam/issue51925.go
1
2
3
4
5
6
7 package main
8
9 import "fmt"
10
11 type IntLike interface {
12 ~int | ~int64 | ~int32 | ~int16 | ~int8
13 }
14
15 func Reduce[T any, U any, Uslice ~[]U](function func(T, U) T, sequence Uslice, initial T) T {
16 result := initial
17 for _, x := range sequence {
18 result = function(result, x)
19 }
20 return result
21 }
22
23 func min[T IntLike](x, y T) T {
24 if x < y {
25 return x
26 }
27 return y
28
29 }
30
31
32 func Min[T IntLike, NumSlice ~[]T](nums NumSlice) T {
33 if len(nums) == 0 {
34 return T(0)
35 }
36 return Reduce(min[T], nums, nums[0])
37 }
38
39
40 func VarMin[T IntLike](nums ...T) T {
41 return Min(nums)
42 }
43
44 type myInt int
45
46 func main() {
47 fmt.Println(VarMin(myInt(1), myInt(2)))
48
49 seq := []myInt{1, 2}
50 fmt.Println(Min(seq))
51 fmt.Println(VarMin(seq...))
52 }
53
View as plain text