Hey! The new draft is way better than previous, but it still allows to write
some tricky code.
func String(type T fmt.Stringer) (x T) string {
return x.String()
}
This is not really a good type parametrization example. Why to have such a
feature? What is the point? What is the difference between it and this code:
func String(x fmt.Stringer) string {
return x.String()
}
I believe it must be not allowed to use specific types in parametrization. Here
is one more example:
func Add(type T int) (x, y T) T {
return x + y
}
This doesn't make any sense.
I think it must be allowed only to use "contracts" as a parametrizing type like
this:
type MyContract interface{
type int, string
}
And it must be denied to use such a contract:
type MyContract interface{
type int
}
Since it doesn't really make sense at all, too. It is only a way to clutter
everything up, and it is not a useful feature.
The second issue is interfaces must serve only on purpose - matching a struct
with some methods. I believe "contracts" must be a separate feature. And my
proposed syntax is:
type MyContract int | string
Another option is to implement it like this:
type MyContract interface int, string
So, it would not allow specifying any methods and could be treated as a
"contract" or "list type" as I call it. It also has way simpler syntax.
It is a way simpler thing, serves exactly one purpose, doesn't confuse and
allows to handle a parametrized types easier.
// Only a "contract" is allowed as a parametrizing type
func Add(type T int | string) ... // direct
func Add(type T MyContract) ... // named
Another feature I would like to propose is a type checker improvement, so it
would allow to access fields of parametrizing types. So You could wirite:
var x T
s := x.Name
I have created my own demo for it: https://playgolang.hulakengineering.com
(click "NAME FUNCTION EXAMPLE")
Thank You for the attention, I hope my proposals will be considered.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/B18DC142-15DA-4058-92BD-C631C8AF109E%40gmail.com.