New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Go 2: extend interface for generic #37594
Comments
When you write // x, y has the same type
func Max(x, y Numeric) Numeric { are you saying that |
For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md . When you are done, please reply to the issue with Thanks! |
contract Numeric(T) {
T int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
}
func Max(type T Numeric)(x , y T) T {
...
}
Max(int)(x, y)
// if type inference is possible, can be write
Max(x, y) Similar with Contracts type T interface {
int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
}
func Max(type T)(x, y T) T {
...
}
// for short can be write
func Max(x, y T) T {
...
} |
No, interfaces and type parameters are very different. When I write func F(x, y interface{}) then When I write func F(type T)(x, y T) then |
Got it. I think contracts is very good, just syntax is complex. // contracts - syntax like function,but body like struct
// func functionName (Parameters)
contract Numeric(T1, T2) {
T1 int, int32 // anotherContract(T1)
T2 float32, float64
}
contract Numeric1(T1, T3) {
T1 int, int32 // anotherContract(T1)
T3 uint, uint32
}
type SomeType(type T1, T2 Numeric) map[T1]T2
func (a SomeType(T1, T2)) M() SomeType(T1, T2)
func(type T1, T2 Numeric)(x T1, y T2)
func(type T1, T3 Numeric1)(x T1, y T3) How about as a new type, seem more flexible. type T1 contract {
int, int32
}
type T2 contract {
float32, float64
}
type T3 contract {
uint, uint32
}
type SomeType(type T1, T2) map[T1]T2
func (a SomeType(type T1, T2)) M() SomeType(type T1, T2)
// short can be write
type SomeType map[T1]T2
func (a SomeType) M() SomeType
func(type T1, T2)(x T1, y T2)
func(type T1, T3)(x T1, y T3)
// short can be write
func(x T1, y T2)
func(x T1, y T3) |
I like the idea that any primitive can be an interface. So, the following could be possible, and for me I'm not sure about type parameter, but the interface type could just be fine: type int interface {
Arithmetic // interface to operators
Comparable // interface to operators
Stringer // interface to operators
// etc..
}
...
type T interface {
int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
}
func Max(x, y T) T {
...
} |
The "etc." leaves out a lot when you start to think about slices, maps, channels, type conversions, untyped constants, slice expressions, index expressions, range loops. |
@gocs If I'm not mistaken, what you say falls under the same pitfalls as the original proposal. For instance, in the following code:
What are the types of |
Hi @deanveloper,
The types of
If you would want to format type T interface {
// primitive type embeddings doesn't break compatibility because interfaces are implemented implicitly and already is.
int // implements greater than operator and others implicitly
int64 // implements greater than operator and others implicitly
float64 // implements greater than operator and others implicitly
}
func Max(a, b T) T {
if a > b { // both variables are comparable because int, int64, and float64 are comparable
return a
}
return b
}
i := Max(1, 1.1)
s := strconv.Format(i, 10) |
I'm aware that there has to be a type between a struct and an interface, and it does not have to be a "Generics".
IMO, this is my check list for a contract-like proposal:
also, only thing holding me back is |
This proposal is motivated by go2draft-contracts.
interface may list explicit types that may be used as type arguments.
math.Max function
before
after
return value type rely on input type
method as default method
Type assertions
The text was updated successfully, but these errors were encountered: