Last active
June 8, 2017 11:55
-
-
Save mh-cbon/e40a8424e95d93ed274ba7227f321b5b to your computer and use it in GitHub Desktop.
generics ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Equer interface { | |
Eq(right Equer) bool | |
} | |
func Eq(left <T:BasicKind|*BasicKind|Equer>, right T) bool { | |
switch T.(kind){ | |
case BasicKind: | |
return left == right | |
case *BasicKind: | |
return left!=nil && right!=nil && *left == *right | |
case Equer: | |
return left.Eq(right) | |
} | |
return false // ? | |
} | |
func SliceEq(left []<T:BasicKind|*BasicKind|Equer>, right []T) bool { | |
if len(left)!=len(right){ | |
return false | |
} | |
for _, l := range left { | |
found := false | |
for _, r := range right { | |
if Eq(l,r) { | |
found = true | |
break | |
} | |
} | |
if found == false { | |
return false | |
} | |
} | |
return true | |
} | |
func RetInput(input <T:Any>) T { | |
return input | |
} | |
func NewSliceOf(input ...<T:Any>) []T { | |
ret := make([]T,0) | |
ret = append(ret, inputs...) | |
return ret | |
} | |
func NewOf(input <T:Any>) Whatever<T> { | |
return Whatever<T>{} | |
} | |
type Concater<T:Struct|*Struct> interface { | |
Concat(right T) T | |
} | |
// type concatenable struct{} | |
// func (c concatenable) Concat(right concatenable) concatenable {return c+right} | |
// xx := Whatever<concatenable>{} | |
// xx := Whatever<string>{Value:"hello"} | |
// xx := Whatever<*string>{} | |
type Whatever<T:StringKind|*StringKind|Concater> struct{ | |
Value T | |
} | |
//type Whatever<T:Any> struct{} | |
func (w Whatever<T>) Hi() string { | |
return fmt.Sprintf(`"Hi, my name is %T"`, w.Value) | |
} | |
func (w *Whatever<T>) Concat(right T) { | |
switch T.(kind){ | |
case StringKind: | |
w.Value = w.Value +right // ? | |
case *StringKind: | |
x := "" | |
if w.Value!=nil{ | |
x = w.Value | |
} | |
if right != nil { | |
x+= *right | |
} | |
w.Value = &x | |
case Concater: | |
w.Value = w.Value.Concat(right) | |
} | |
} | |
func ManyTypes(x <T:Any>, xx <U:Any>) (T,U) { | |
return x,xx | |
} | |
// something like that maybe... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment