proposal: spec: allow selective method embedding for interface definitions (like TypeScript's Pick
)
#73373
Closed
1 of 4 tasks
Labels
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
LanguageProposal
Issues describing a requested change to the Go language specification.
Proposal
Milestone
Go Programming Experience
Intermediate
Other Languages Experience
No response
Related Idea
Has this idea, or one like it, been proposed before?
No - I was unable to find related proposals. I would expect there to be existing proposals, but was unable to find any. Please link if I am mistaken, I'd be interested in reading discussion around this.
Does this affect error handling?
No
Is this about generics?
No
Proposal
Background:
Effective Go encourages the use of defining small, consumer-oriented interfaces. In practice, this results in some toil in copying signatures as well as importing the necessary paths to correctly define those signatures.
For example:
Now a consumer-defined interface must match this declaration and imports:
While not a large amount of toil, the developer's goal will very frequently be to declare the exact same signature from some type, so there is some opportunity to simplify this.
Proposal:
Introduce syntax for consumer-side interfaces to selectively include methods from existing interfaces or types (similar to TypeScript's
Pick
), e.g.:This allows defining
ConsumerInterface
with just theRead
method fromio.ReadCloser
andWrite
from*os.File
.Pros:
Drawbacks:
go doc
,gopls
)Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
Yes
Orthogonality: How does this change interact or overlap with existing features?
Go currently allows for the composition of interface types through interface embedding:
This proposal is similarly for the composition of an interface, but through selective embedding.
Would this change make Go easier or harder to learn, and why?
Yes. It is another feature and a new way to compose an interface.
Cost Description
go doc
,gopls
)Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: