-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerics_test.go
65 lines (56 loc) · 1.19 KB
/
generics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package julia
import (
"testing"
)
// data struct is type-parametrized
type data[T PrimitiveTypes] struct {
values []T
name string
}
// Len is a method on data parametrized by T
func (g *data[T]) Len() int {
return len(g.values)
}
func (g *data[T]) IsInt32() bool {
var el T
switch any(el).(type) {
case int32:
return true
default:
return false
}
}
// IsSameType checks sameness of type parameter inputs by
// using a type switch on a new instance of a variable.
// type switches cannot be used on variable types constrained
// by type parameters, hence it needs to be wrapped in an
// interface such as any
func IsSameType[T, W PrimitiveTypes]() bool {
var el T
switch any(el).(type) {
case W:
return true
default:
return false
}
}
func newData[T PrimitiveTypes](x []T, name string) *data[T] {
return &data[T]{
values: x,
name: name,
}
}
func TestLen(t *testing.T) {
d := newData([]int32{1, 2, 3}, "data")
if d.Len() != 3 {
t.Fatal("expected length to be 3 using method")
}
}
func TestIsSameType(t *testing.T) {
if IsSameType[int32, float64]() {
t.Fatal("expected types to be different")
}
if !IsSameType[int32, int32]() {
t.Fatal("expected types to be the same")
}
}