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 types out of package where it is defined. #37742
Comments
This has been suggested before, though I can't locate any earlier conversations. The problem is interface types. If a type is defined in package a, adding a method in package b can mean that the type implements interfaces that it did not implement before. That means that conversions to interface type will change. When should it change? Should all types created in package a now implement the new interface? How should conversion in and out of |
Moreover, this makes methods hard to locate. If I have a foo.Bar them now I can be sure that all methods of Bar are defined in the package foo, and that there can be no others. |
Previous discussion |
Perhaps a different take to solve this would be to build on top of the underlying type syntax. In Go, you can create a type from an underlying type, but lose the underlying type's methods: type Foo bar.Bar You can also create a type alias which "inherits" the methods of the "underlying" type, because that is not really a new type: type Foo = bar.Bar For discussion's sake, what about creating new types from an underlying type, which also "inherit" that type's method, and perhaps, implicit conversion between the new type and extended type can also be allowed: type Foo + bar.Bar
|
This is indeed a problem! Can we create a virtual type for |
Maybe we do some trade off, just refer rust implementation, rust can add new method in |
Thanks for the suggestions, but all of them have limits. |
@urandom this thing with + would mean: if I "derive" my type from e.g. int, I could use operators like + in my "derived" type? - I don't think so. Unless operators are handled like methods or interfaces. |
@maj-o you already can use operators that work on your underlying type with your type |
Based on the discussion about this is similar to #21401 which was closed. Therefore this is a likely decline. Leaving open for four weeks for final comments. |
Would love to have this. |
No change in consensus. Closing. |
In modorn language, like
rust
,swift
,dart
, programers can extends types in user code. Inrust
, it'simpl Foo for Bar
, inswift
, it'sextension Foo
, in dart, it'sextension Foo on Bar
. The extension syntax give ability to extend any types in a simple way. It's powerful and useful. But now in Go, we can't achieve this any way.In
Rust
way:In
Swift
way:In
Dart
way:Existing way.
The existing way to achieve this is define new types or embed types in another types.
1. Define new types. For example:
in package a we have a type A:
in package b, if we want extend
a.A
, we must define a new typesand then:
but when using
a.A
, we must transform tob.B
, it's annoying and not required if we have the ability to extend types out of package where it is defined.2. Another way is embed in another type
in package
a
and in package
b
then
it's also need wrap
a.A
tob.B
to give an extended methodFoo()
.Proposal way
At this background, I proposal add ability to extend types out of package where it's defined. There are some syntax.
1. Normal syntax without any breaking syntax.
the only change is
*A
->*a.A
.2. Add new keyword like
extend
orext
.the
extend
keyword beforefunc
is to describe methodFoo
andBar
is extension method.3. Like 2 add new keyword but place it before method name.
the
+
means this method is additional.Usage
The above syntax give some way to achieve this, the following will give example how to use it.
1. use old
_
import syntaxThe text was updated successfully, but these errors were encountered: