Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
proposal: Go 2: spec: add `self` type for use within interfaces #28254
Let's make an interface called
As any good Go interface should be designed, it should describe behaviors rather than making implementations conform to the interface.
So, let's see our implementations:
As you can see, the two
We could "solve" this issue like so:
Then, we'd design the
This causes a few issues, though.
This is also apparently not the most intuitive solution, as it needs to be answered in the FAQ: https://golang.org/doc/faq#t_and_equal_interface
This is "solved" by generics, although not really. This is detailed at the bottom of the proposal.
The proposed solution:
There is a workaround for this problem within the existing type system. To have a clonable interface
you just make the method for cloning have a name specific to
Then, to implement with a concrete struct
It is not as succinct as
In my experience, real uses of
It's noteworthy that many other type systems are much more complicated than Go's; when you have things like covariance then the
What you have provided isn't a terrible solution to the issue. It still suffers from designing your structs around the interface, though, rather than designing the interface around a pattern. For instance, I wouldn't be able to describe a recurring pattern in a foreign package.
I'm not clear on whether this can be implemented. Presumably
It's also worth noting that
I'm not sure what you mean here. The package where the conversion occurs has access to the type descriptor for
(I suspect we could even write such a function using the
So basically you want to replace interface name in parameter type as "self" word.
Does the current solution not suit your needs? Why such complication?
This will affect changes in existing editors, libraries etc. Also "self" word is so "pythonish". Let's go more in C/C++ direction than Python.
No, it's not the interface name
Look at the Cloner interface that I have provided, it matches