diff --git a/pkg/typex/generic.go b/pkg/typex/generic.go new file mode 100644 index 0000000..18cc1b7 --- /dev/null +++ b/pkg/typex/generic.go @@ -0,0 +1,8 @@ +package typex + +import "reflect" + +func TypeOf[T any]() reflect.Type { + var t T + return reflect.TypeOf(t) +} diff --git a/pkg/typex/multitype.go b/pkg/typex/multitype.go index 12ac6c4..a834346 100644 --- a/pkg/typex/multitype.go +++ b/pkg/typex/multitype.go @@ -5,12 +5,16 @@ import "reflect" type MultiType2[T1, T2 any] struct { obj any t reflect.Type + t1 reflect.Type + t2 reflect.Type } func NewMultiType2WithT1[T1, T2 any](obj T1) *MultiType2[T1, T2] { return &MultiType2[T1, T2]{ obj: obj, t: reflect.TypeOf(obj), + t1: TypeOf[T1](), + t2: TypeOf[T2](), } } @@ -18,15 +22,17 @@ func NewMultiType2WithT2[T1, T2 any](obj T2) *MultiType2[T1, T2] { return &MultiType2[T1, T2]{ obj: obj, t: reflect.TypeOf(obj), + t1: TypeOf[T1](), + t2: TypeOf[T2](), } } func (m *MultiType2[T1, T2]) IsT1() bool { - return m.t == reflect.TypeOf(m.obj) + return m.t == m.t1 } func (m *MultiType2[T1, T2]) IsT2() bool { - return m.t == reflect.TypeOf(m.obj) + return m.t == m.t2 } func (m *MultiType2[T1, T2]) GetT1() T1 { @@ -39,10 +45,12 @@ func (m *MultiType2[T1, T2]) GetT2() T2 { func (m *MultiType2[T1, T2]) SetT1(t T1) { m.obj = t + m.t = m.t1 } func (m *MultiType2[T1, T2]) SetT2(t T2) { m.obj = t + m.t = m.t2 } func (m *MultiType2[T1, T2]) Get() any { @@ -52,12 +60,18 @@ func (m *MultiType2[T1, T2]) Get() any { type MultiType3[T1, T2, T3 any] struct { obj any t reflect.Type + t1 reflect.Type + t2 reflect.Type + t3 reflect.Type } func NewMultiType3WithT1[T1, T2, T3 any](obj T1) *MultiType3[T1, T2, T3] { return &MultiType3[T1, T2, T3]{ obj: obj, t: reflect.TypeOf(obj), + t1: TypeOf[T1](), + t2: TypeOf[T2](), + t3: TypeOf[T3](), } } @@ -65,6 +79,9 @@ func NewMultiType3WithT2[T1, T2, T3 any](obj T2) *MultiType3[T1, T2, T3] { return &MultiType3[T1, T2, T3]{ obj: obj, t: reflect.TypeOf(obj), + t1: TypeOf[T1](), + t2: TypeOf[T2](), + t3: TypeOf[T3](), } } @@ -72,19 +89,22 @@ func NewMultiType3WithT3[T1, T2, T3 any](obj T3) *MultiType3[T1, T2, T3] { return &MultiType3[T1, T2, T3]{ obj: obj, t: reflect.TypeOf(obj), + t1: TypeOf[T1](), + t2: TypeOf[T2](), + t3: TypeOf[T3](), } } func (m *MultiType3[T1, T2, T3]) IsT1() bool { - return m.t == reflect.TypeOf(m.obj) + return m.t == m.t1 } func (m *MultiType3[T1, T2, T3]) IsT2() bool { - return m.t == reflect.TypeOf(m.obj) + return m.t == m.t2 } func (m *MultiType3[T1, T2, T3]) IsT3() bool { - return m.t == reflect.TypeOf(m.obj) + return m.t == m.t3 } func (m *MultiType3[T1, T2, T3]) GetT1() T1 { @@ -101,14 +121,18 @@ func (m *MultiType3[T1, T2, T3]) GetT3() T3 { func (m *MultiType3[T1, T2, T3]) SetT1(t T1) { m.obj = t + m.t = m.t1 } func (m *MultiType3[T1, T2, T3]) SetT2(t T2) { m.obj = t + m.t = m.t2 + } func (m *MultiType3[T1, T2, T3]) SetT3(t T3) { m.obj = t + m.t = m.t3 } func (m *MultiType3[T1, T2, T3]) Get() any { diff --git a/pkg/typex/multitype_test.go b/pkg/typex/multitype_test.go new file mode 100644 index 0000000..198616c --- /dev/null +++ b/pkg/typex/multitype_test.go @@ -0,0 +1,30 @@ +package typex + +import ( + "testing" +) + +func TestMultiType(t *testing.T) { + m := NewMultiType2WithT1[double, string](1) + if !m.IsT1() { + t.Errorf("IsT1 failed") + t.Fail() + } + if m.IsT2() { + t.Errorf("IsT2 failed") + t.Fail() + } + m.SetT2("hello") + if !m.IsT2() { + t.Errorf("IsT2 failed") + t.Fail() + } + if m.IsT1() { + t.Errorf("IsT1 failed") + t.Fail() + } + if m.GetT2() != "hello" { + t.Errorf("GetT2 failed") + t.Fail() + } +} diff --git a/pkg/typex/num.go b/pkg/typex/num.go index 7662197..3c4b415 100644 --- a/pkg/typex/num.go +++ b/pkg/typex/num.go @@ -10,3 +10,4 @@ type u32 uint32 type u64 uint64 type f32 float32 type f64 float64 +type double float64