|
| 1 | +package generics |
| 2 | + |
| 3 | +// Generic type with single type parameter |
| 4 | +// String:"Box[T any]" |
| 5 | +// NumTypeParam:"1" |
| 6 | +// To:"TypeParam:0" Name:"T" Kind:"TypeParam" String:"T any" |
| 7 | +// To:"TypeParam:0,Constraint" Name:"any" |
| 8 | +type Box[T any] struct { |
| 9 | + Value T |
| 10 | +} |
| 11 | + |
| 12 | +// Generic type with constraint |
| 13 | +// String:"Comparable[T comparable]" |
| 14 | +// NumTypeParam:"1" |
| 15 | +// To:"TypeParam:0" Name:"T" Kind:"TypeParam" String:"T comparable" |
| 16 | +// To:"TypeParam:0,Constraint" Name:"comparable" |
| 17 | +type Comparable[T comparable] struct { |
| 18 | + Value T |
| 19 | +} |
| 20 | + |
| 21 | +// Generic type with multiple type parameters |
| 22 | +// String:"Pair[K comparable, V any]" |
| 23 | +// NumTypeParam:"2" |
| 24 | +// To:"TypeParam:0" Name:"K" Kind:"TypeParam" String:"K comparable" |
| 25 | +// To:"TypeParam:0,Constraint" Name:"comparable" |
| 26 | +// To:"TypeParam:1" Name:"V" Kind:"TypeParam" String:"V any" |
| 27 | +// To:"TypeParam:1,Constraint" Name:"any" |
| 28 | +type Pair[K comparable, V any] struct { |
| 29 | + Key K |
| 30 | + Value V |
| 31 | +} |
| 32 | + |
| 33 | +// Generic function |
| 34 | +// String:"Max[T comparable]" |
| 35 | +// To:"Declaration" NumTypeParam:"1" String:"func[T comparable](a, b) (_)" |
| 36 | +// To:"Declaration,TypeParam:0" Name:"T" Kind:"TypeParam" String:"T comparable" |
| 37 | +// To:"Declaration,TypeParam:0,Constraint" Name:"comparable" |
| 38 | +func Max[T comparable](a, b T) T { |
| 39 | + if a > b { |
| 40 | + return a |
| 41 | + } |
| 42 | + return b |
| 43 | +} |
| 44 | + |
| 45 | +// Generic function with multiple type parameters |
| 46 | +// String:"Map[K comparable, V any]" |
| 47 | +// To:"Declaration" NumTypeParam:"2" String:"func[K comparable, V any](m, f) (_)" |
| 48 | +// To:"Declaration,TypeParam:0" Name:"K" Kind:"TypeParam" String:"K comparable" |
| 49 | +// To:"Declaration,TypeParam:0,Constraint" Name:"comparable" |
| 50 | +// To:"Declaration,TypeParam:1" Name:"V" Kind:"TypeParam" String:"V any" |
| 51 | +// To:"Declaration,TypeParam:1,Constraint" Name:"any" |
| 52 | +func Map[K comparable, V any](m map[K]V, f func(V) V) map[K]V { |
| 53 | + result := make(map[K]V) |
| 54 | + for k, v := range m { |
| 55 | + result[k] = f(v) |
| 56 | + } |
| 57 | + return result |
| 58 | +} |
| 59 | + |
| 60 | +// Custom interface constraint |
| 61 | +type Stringer interface { |
| 62 | + String() string |
| 63 | +} |
| 64 | + |
| 65 | +// Generic type with custom interface constraint |
| 66 | +// String:"StringableBox[T Stringer]" |
| 67 | +// NumTypeParam:"1" |
| 68 | +// To:"TypeParam:0" Name:"T" Kind:"TypeParam" String:"T Stringer" |
| 69 | +// To:"TypeParam:0,Constraint" Name:"Stringer" Kind:"Interface" |
| 70 | +type StringableBox[T Stringer] struct { |
| 71 | + Value T |
| 72 | +} |
| 73 | + |
| 74 | +// Generic function with custom interface constraint |
| 75 | +// String:"Stringify[T Stringer]" |
| 76 | +// To:"Declaration" NumTypeParam:"1" String:"func[T Stringer](value) (_)" |
| 77 | +// To:"Declaration,TypeParam:0" Name:"T" Kind:"TypeParam" String:"T Stringer" |
| 78 | +// To:"Declaration,TypeParam:0,Constraint" Name:"Stringer" Kind:"Interface" |
| 79 | +func Stringify[T Stringer](value T) string { |
| 80 | + return value.String() |
| 81 | +} |
0 commit comments