golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
120.67k stars 17.33k forks source link

all: support gradual code repair while moving a type between packages #18130

Closed rsc closed 7 years ago

rsc commented 7 years ago

Original Title: proposal: support gradual code repair while moving a type between packages

Go should add the ability to create alternate equivalent names for types, in order to enable gradual code repair during codebase refactoring. This was the target of the Go 1.8 alias feature, proposed in #16339 but held back from Go 1.8. Because we did not solve the problem for Go 1.8, it remains a problem, and I hope we can solve it for Go 1.9.

In the discussion of the alias proposal, there were many questions about why this ability to create alternate names for types in particular is important. As a fresh attempt to answer those questions, I wrote and posted an article, “Codebase Refactoring (with help from Go).” Please read that article if you have questions about the motivation. (For an alternate, shorter presentation, see Robert's Gophercon lightning talk. Unfortunately, that video wasn't available online until October 9. Update, Dec 16: here's my GothamGo talk, which was essentially the first draft of the article.)

This issue is not proposing a specific solution. Instead, I want to gather feedback from the Go community about the space of possible solutions. One possible avenue is to limit aliases to types, as mentioned at the end of the article. There may be others we should consider as well.

Please post thoughts about type aliases or other solutions as comments here.

Thank you.

Update, Dec 16: Design doc for type aliases posted. Update, Jan 9: Proposal accepted, dev.typealias repository created, implementation due at the start of the Go 1.9 cycle for experimentation.


Discussion summary (last updated 2017-02-02)

Do we expect to need a general solution that works for all declarations?

If type aliases are 100% necessary, then var aliases are maybe 10% necessary, func aliases are 1% necessary, and const aliases are 0% necessary. Because const already has = and func could plausibly use = too, the key question is whether var aliases are important enough to plan for or implement.

As argued by @rogpeppe (https://github.com/golang/go/issues/16339#issuecomment-258771806) and @ianlancetaylor (https://github.com/golang/go/issues/16339#issuecomment-233644777) in the original alias proposal and as mentioned in the article, a mutating global var is usually a mistake. It probably doesn't make sense to complicate the solution to accommodate what is usually a bug. (In fact, if we can figure out how, it would not surprise me if in the long term Go moves toward requiring global vars to be immutable.)

Because richer var aliases are likely not important enough to plan for, it seems like the right choice here is to focus only on type aliases. Most of the comments here seem to agree. I won't list everyone.

Do we need a new syntax (= vs => vs export)?

The strongest argument for new syntax is the need to support var aliases, either now or in the future (https://github.com/golang/go/issues/18130#issuecomment-264232763 by @Merovius). It seems okay to plan not to have var aliases (see previous section).

Without var aliases, reusing = is simpler than introducing new syntax, whether => like in the alias proposal, ~ (https://github.com/golang/go/issues/18130#issuecomment-264185142 by @joegrasse), or export (https://github.com/golang/go/issues/18130#issuecomment-264152427 by @cznic).

Using = in would also exactly match the syntax of type aliases in Pascal and Rust. To the extent that other languages have the same concepts, it's nice to use the same syntax.

Looking ahead, there could be a future Go in which func aliases exist too (see https://github.com/golang/go/issues/18130#issuecomment-264324306 by @nigeltao), and then all declarations would permit the same form:

const C2 = C1
func F2 = F1
type T2 = T1
var V2 = V1

The only one of these that wouldn't establish a true alias would be the var declaration, because V2 and V1 can be redefined independently as the program executes (unlike the const, func, and type declarations which are immutable). Since one main reason for variables is to allow them to vary, that exception would at least be easy to explain. If Go moves toward immutable global vars, then even that exception would disappear.

To be clear, I am not suggesting func aliases or immutable global vars here, just working through the implications of such future additions.

@jimmyfrasche suggested (https://github.com/golang/go/issues/18130#issuecomment-264278398) aliases for everything except consts, so that const would be the exception instead of var:

const C2 = C1 // no => form
func F2 => F1
type T2 => T1
var V2 => V1
var V2 = V1 // different from => form

Having inconsistencies with both const and var seems more difficult to explain than having just an inconsistency for var.

Can this be a tooling- or compiler-only change instead of a language change?

It's certainly worth asking whether gradual code repair can be enabled purely by side information supplied to the compiler (for example, https://github.com/golang/go/issues/18130#issuecomment-264205929 by @btracey).

Or maybe if the compiler can apply some kind of rule-based preprocessing to transform input files before compilation (for example, https://github.com/golang/go/issues/18130#issuecomment-264329924 by @tux21b).

Unfortunately, no, the change really can't be confined that way. There are at least two compilers (gc and gccgo) that would need to coordinate, but so would any other tools that analyze programs, like go vet, guru, goimports, gocode (code completion), and others.

As @bcmills said (https://github.com/golang/go/issues/18130#issuecomment-264275574), “a ‘non-language-change’ mechanism which must be supported by all implementations is a de facto language change — it’s just one with poorer documentation.”

What other uses might aliases have?

We know of the following. Given that type aliases in particular were deemed important enough for inclusion in Pascal and Rust, there are likely others.

  1. Aliases (or just type aliases) would enable creating drop-in replacements that expand other packages. For example see https://go-review.googlesource.com/#/c/32145/, especially the explanation in the commit message.

  2. Aliases (or just type aliases) would enable structuring a package with a small API surface but a large implementation as a collection of packages for better internal structure but still present just one package to be imported and used by clients. There's a somewhat abstract example described at https://github.com/golang/go/issues/16339#issuecomment-232813695.

  3. Protocol buffers have an "import public" feature whose semantics is trivial to implement in generated C++ code but impossible to implement in generated Go code. This causes frustration for authors of protocol buffer definitions shared between C++ and Go clients. Type aliases would provide a way for Go to implement this feature. In fact, the original use case for import public was gradual code repair. Similar issues may arise in other kinds of code generators.

  4. Abbreviating long names. Local (unexported or not-package-scoped) aliases might be handy to abbreviate a long type name without introducing the overhead of a whole new type. As with all these uses, the clarity of the final code would strongly influence whether this is a suggested use.

What other issues does a proposal for type aliases need to address?

Listing these for reference. Not attempting to solve or discuss them in this section, although a few were discussed later and are summarized in separate sections below.

  1. Handling in godoc. (https://github.com/golang/go/issues/18130#issuecomment-264323137 by @nigeltao and https://github.com/golang/go/issues/18130#issuecomment-264326437 by @jimmyfrasche)

  2. Can methods be defined on types named by alias? (https://github.com/golang/go/issues/18130#issuecomment-265077877 by @ulikunitz)

  3. If aliases to aliases are allowed, how do we handle alias cycles? (https://github.com/golang/go/issues/18130#issuecomment-264494658 by @thwd)

  4. Should aliases be able to export unexported identifiers? (https://github.com/golang/go/issues/18130#issuecomment-264494658 by @thwd)

  5. What happens when you embed an alias (how do you access the embedded field)? (https://github.com/golang/go/issues/18130#issuecomment-264494658 by @thwd, also #17746)

  6. Are aliases available as symbols in the built program? (https://github.com/golang/go/issues/18130#issuecomment-264494658 by @thwd)

  7. Ldflags string injection: what if we refer to an alias? (https://github.com/golang/go/issues/18130#issuecomment-264494658 by @thwd; this only arises if there are var aliases.)

Is versioning a solution by itself?

"In that case maybe versioning is the whole answer, not type aliases." (https://github.com/golang/go/issues/18130#issuecomment-264573088 by @iainmerrick)

As noted in the article, I think versioning is an complementary concern. Support for gradual code repair, such as with type aliases, gives a versioning system more flexibility in how it builds a large program, which can be difference between being able to build the program and not.

Can the larger refactoring problem be solved instead?

In https://github.com/golang/go/issues/18130#issuecomment-265052639, @niemeyer points out that there were actually two changes for moving os.Error to error: the name changed but so did the definition (the current Error method used to be a String method).

@niemeyer suggests that perhaps we can find a solution to the broader refactoring problem that fixes types moving between packages as a special case but also handles things like method names changing, and he proposes a solution built around "adapters".

There is a fair amount of discussion in the comments that I can't easily summarize here. The discussion isn't over, but so far it is unclear whether "adapters" can fit into the language or be implemented in practice. It does seem clear that adapters are at least one order of magnitude more complex than type aliases.

Adapters need a coherent solution to the subtyping problems noted below as well.

Can methods be declared on alias types?

Certainly aliases do not allow bypassing the usual method definition restrictions: if a package defines type T1 = otherpkg.T2, it cannot define methods on T1, just as it cannot define methods directly on otherpkg.T2. That is, if type T1 = otherpkg.T2, then func (T1) M() is equivalent to func (otherpkg.T2) M(), which is invalid today and remains invalid. However, if a package defines type T1 = T2 (both in the same package), then the answer is less clear. In this case, func (T1) M() would be equivalent to func (T2) M(); since the latter is allowed, there is an argument to allow the former. The current design doc does not impose a restriction here (in keeping with the general avoidance of restrictions), so that func (T1) M() is valid in this situation.

In https://github.com/golang/go/issues/18130#issuecomment-267694112, @jimmyfrasche suggests that instead defining "no use of aliases in method definitions" would be a clear rule and avoid needing to know what T is defined as to know if func (T) M() is valid. In https://github.com/golang/go/issues/18130#issuecomment-267997124, @rsc points out that even today there are certain T for which func (T) M() is not valid: https://play.golang.org/p/bci2qnldej. In practice this doesn't come up because people write reasonable code.

We will keep this possible restriction in mind but wait until there is strong evidence that it is needed before introducing it.

Is there a cleaner way to handle embedding and, more generally, field renames?

In https://github.com/golang/go/issues/18130#issuecomment-267691816, @Merovius points out that an embedded type that changes its name during a package move will cause problems when that new name must eventually be adopted at the use sites. For example if user type U has an embedded io.ByteBuffer that moves to bytes.Buffer, then while U embeds io.ByteBuffer the field name is U.ByteBuffer, but when U is updated to refer to bytes.Buffer, the field name necessarily changes to U.Buffer.

In https://github.com/golang/go/issues/18130#issuecomment-267710478, @neild points out that there is at least a workaround if references to io.ByteBuffer must be excised: the package P that defines U can also define 'type ByteBuffer = bytes.Buffer' and embed that type into U. Then U still has a U.ByteBuffer, even after io.ByteBuffer is gone entirely.

In https://github.com/golang/go/issues/18130#issuecomment-267703067, @bcmills suggests the idea of field aliases, to allow a field to have multiple names during a gradual repair. Field aliases would allow defining something like type U struct { bytes.Buffer; ByteBuffer = Buffer } instead of having to create the top-level type alias.

In https://github.com/golang/go/issues/18130#issuecomment-268001111, @rsc raises yet another possibility: some syntax for 'embed this type with this name', so that it is possible to embed a bytes.Buffer as the field name ByteBuffer, without needing a top-level type or an alternate name. If that existed, then the type name could be updated from io.ByteBuffer to bytes.Buffer while preserving the original name (and not introducing a second, nor a clumsy exported type).

These all seem worth exploring once we have more evidence of large-scale refactorings blocked by problems with fields changing names. As @rsc wrote, "If type aliases help us get to the point where lack of field aliases is the next big roadblock for large-scale refactorings, that will be progress!"

There was a suggestion of restricting the use of aliases in embedded fields or changing the embedded name to use the target type's name, but those make the alias introduction break existing definitions that must then be fixed atomically, essentially preventing any gradual repair. @rsc: "We discussed this at some length in #17746. I was originally on the side of the name of an embedded io.ByteBuffer alias being Buffer, but the above argument convinced me I was wrong. @jimmyfrasche in particular made some good arguments about the code not changing depending on the definition of the embedded thing. I don't think it's tenable to disallow embedded aliases completely."

What is the effect on programs using reflection?

Programs using reflection see through aliases. In https://github.com/golang/go/issues/18130#issuecomment-267903649, @atdiar points out that if a program is using reflection to, for example, find the package in which a type is defined or even the name of a type, it will observe the change when the type is moved, even if a forwarding alias is left behind. In https://github.com/golang/go/issues/18130#issuecomment-268001410, @rsc confirmed this and wrote "Like the situation with embedding, it's not perfect. Unlike the situation with embedding, I don't have any answers except maybe code shouldn't be written using reflect to be quite that sensitive to those details."

The use of vendored packages today also changes package import paths seen by reflect, and we have not been made aware of significant problems caused by that ambiguity. This suggests that programs are not commonly inspecting reflect.Type.PkgPath in ways that would be broken by use of aliases. Even so, it's a potential gap, just like embedding.

What is the effect on separate compilation of programs and plugins?

In https://github.com/golang/go/issues/18130#issuecomment-268524504, @atdiar raises the question of the effect on object files and separate compilation. In https://github.com/golang/go/issues/18130#issuecomment-268560180, @rsc replies that there should be no need to make changes here: if X imports Y and Y changes and is recompiled, then X needs to be recompiled too. That's true today without aliases, and it will remain true with aliases. Separate compilation means being able to compile X and Y in distinct steps (the compiler does not have to process them in the same invocation), not that it is possible to change Y without recompiling X.

Would sum types or some kind of subtyping be an alternative solution?

In https://github.com/golang/go/issues/18130#issuecomment-264413439, @iand suggests "substitutable types", "a list of types that may be substituted for the named type in function arguments, return values etc.". In https://github.com/golang/go/issues/18130#issuecomment-268072274, @j7b suggests using algebraic types "so we also get an empty interface equivalent with compile time type checking as a bonus". Other names for this concept are sum types and variant types.

In general this does not suffice to allow moving types with gradual code repair. There are two ways to think about this.

In https://github.com/golang/go/issues/18130#issuecomment-268075680, @bcmills takes the concrete way, pointing out that algebraic types have a different representation than the original, which makes it not possible to treat the sum and the original as interchangeable: the latter has type tags.

In https://github.com/golang/go/issues/18130#issuecomment-268585497, @rsc takes the theoretical way, expanding on https://github.com/golang/go/issues/18130#issuecomment-265211655 by @gri pointing out that in a gradual code repair, sometimes you need T1 to be a subtype of T2 and sometimes vice versa. The only way for both to be subtypes of each other is for them to be the same type, which not concidentally is what type aliases do.

As a side tangent, in addition to not solving the gradual code repair problem, algebraic types / sum types / union types / variant types are by themselves hard to add to Go. See the FAQ answer and the Go 1.6 AMA discussion for more.

In https://github.com/golang/go/issues/18130#issuecomment-265206780, @thwd suggests that since Go has a subtyping relationship between concrete types and interfaces (bytes.Buffer can be seen as a subtype of io.Reader) and between interfaces (io.ReadWriter is a subtype of io.Reader in the same way), making interfaces "recursively covariant (according to the current variance rules) down to their method arguments" would solve the problem provided that all future packages only use interfaces, never concrete types like structs ("encourages good design, too").

There are three problems with that as a solution. First, it has the subtyping issues above, so it doesn't solve gradual code repair. Second, it doesn't apply to existing code, as @thwd noted in this suggestion. Third, forcing the use of interfaces everywhere may not actually be good design and introduces performance overheads (see for example https://github.com/golang/go/issues/18130#issuecomment-265211726 by @Merovius and https://github.com/golang/go/issues/18130#issuecomment-265224652 by @zombiezen).

Restrictions

This section collects proposed restrictions for reference, but keep in mind that restrictions add complexity. As I wrote in https://github.com/golang/go/issues/18130#issuecomment-264195616, "we should probably only implement those restrictions after actual experience with the unrestricted, simpler design helps us understand whether the restriction would bring enough benefits to pay for its cost."

Put another way, any restriction would need to be justified by evidence that it would prevent some serious misuse or confusion. Since we haven't implemented a solution yet, there is no such evidence. If experience did provide that evidence, these will be worth returning to.

Restriction? Aliases of standard library types can only be declared in standard library.

(https://github.com/golang/go/issues/18130#issuecomment-264165833 and https://github.com/golang/go/issues/18130#issuecomment-264171370 by @iand)

The concern is "code that has renamed standard library concepts to fit a custom naming convention", or "long spaghetti chains of aliases across multiple packages that end up back at the standard library", or "aliasing things like interface{} and error".

As stated, the restriction would disallow the "extension package" case described above involving x/image/draw.

It's unclear why the standard library should be special: the problems would exist with any code. Also, neither interface{} nor error is a type from the standard library. Rephrasing the restriction as "aliasing predefined types" would disallow aliasing error, but the need to alias error was one of the motivating examples in the article.

Restriction? Alias target must be package-qualified identifier.

(https://github.com/golang/go/issues/18130#issuecomment-264188282 by @jba)

This would make it impossible to make an alias when renaming a type within a package, which may be used widely enough to necessitate a gradual repair (https://github.com/golang/go/issues/18130#issuecomment-264274714 by @bcmills).

It would also disallow aliasing error as in the article.

Restriction? Alias target must be package-qualified identifier with same name as alias.

(proposed during alias discussion in Go 1.8)

In addition to the problems of the previous section with limiting to package-qualified identifiers, forcing the name to stay the same would disallow the conversion from io.ByteBuffer to bytes.Buffer in the article.

Restriction? Aliases should be discouraged in some way.

"How about hiding aliases behind an import, just like for "C" and “unsafe”, to further discourage it's usage? In the same vein, I would like the aliases syntax to be verbose and stand out as a scaffold for on going refactoring." - https://github.com/golang/go/issues/18130#issuecomment-264289940 by @xiegeo

"Should we also automatically infer that an aliased type is legacy and should be replaced by the new type? If we enforce golint, godoc and similar tools to visualize the old type as deprecated, it would limit the abuse of type aliasing very significantly. And the final concern of aliasing feature being abused would be resolved." - https://github.com/golang/go/issues/18130#issuecomment-265062154 by @rakyll

Until we know that they will be used wrong, it seems premature to discourage usage. There may be good, non-temporary uses (see above).

Even in the case of code repair, either the old or new type may be the alias during the transition, depending on the constraints imposed by the import graph. Being an alias does not mean the name is deprecated.

There is already a mechanism for marking certain declarations as deprecated (see https://github.com/golang/go/issues/18130#issuecomment-265294564 by @jimmyfrasche).

Restriction? Aliases must target named types.

"Aliases shouldn't not apply to unnamed type. Their is no "code repair" story in moving from one unnamed type to another. Allowing aliases on unnamed types means I can no longer teach Go as simply named and unnamed types." - https://github.com/golang/go/issues/18130#issuecomment-276864903 by @davecheney

Until we know that they will be used wrong, it seems premature to discourage usage. There may be good uses with unnamed targets (see above).

As noted in the design doc, we do expect to change the terminology to make the situation clearer.

variadico commented 7 years ago

I like how visually uniform this looks.

const OldAPI => NewPackage.API
func  OldAPI => NewPackage.API
var   OldAPI => NewPackage.API
type  OldAPI => NewPackage.API

But since we can almost gradually move most elements, maybe the simplest solution is just to allow an = for types.

const OldAPI = NewPackage.API
func  OldAPI() { NewPackage.API() }
var   OldAPI = NewPackage.API
type  OldAPI = NewPackage.API
zquestz commented 7 years ago

So first, I just wanted to thank you for that excellent write-up. I think the best solution is to introduce type aliases with an assignment operator. This requires no new keywords/operators, uses a familiar syntax, and should solve the refactoring problem for large code bases.

iand commented 7 years ago

As Russ's article points out, any alias-like solution needs to gracefully solve https://github.com/golang/go/issues/17746 and https://github.com/golang/go/issues/17784

travisjeffery commented 7 years ago

Thank you for the write up of that article.

I find the type-only aliases using the assignment operator to be best:

type OldAPI = NewPackage.API

My reasons:

All of these above: the result being simple, focused, conservative, and aesthetic make it easy for me to picture of it being a part of Go.

cznic commented 7 years ago

If the solution would be limited to types only then the syntax

type NewFoo = old.Foo

already considered before, as discussed in the @rsc's article, looks very good to me.

If we would like to be able to do the same for constants, variables and functions, my preferred syntax would be (as proposed before)

package newfmt

import (
    "fmt"
)

// No renaming.
export fmt.Printf // Note: Same as `export Printf fmt.Printf`.

export (
        fmt.Sprintf
        fmt.Formatter
)

// Renaming.
export Foo fmt.Errorf // Foo must be exported, ie. `export foo fmt.Errorf` would be invalid.

export (
    Bar fmt.Fprintf
    Qux fmt.State
)

As discussed before, the disadvantage is that a new, top-level only keyword is introduced, which is admittedly akward, even though technically feasible and fully backwards compatible. I like this syntax because it reflects the pattern of imports. It would seem natural to me that exports would be permitted only in the same section where imports are allowed, ie. between the package clause and any var, type, constant or function TLD.

The renaming identifiers would be declared in the package scope, however, the new names are not visible in the package declaring them (newfmt in the example above) above with respect to redeclaration, which is disallowed as usual. Given the previous example, TLDs

var v = Printf // undefined: Printf.
var Printf int // Printf redeclared, previous declaration at newfmt.go:8.

In the importing package the renaming identifiers are visible normally, as any other exported identifier of the (newftm's) package block.

package foo

import "newfmt"

type bar interface {
    baz(qux newfmt.Qux) // qux type is identical to fmt.State.
}

In conclusion, this approach does not introduce any new local name binding in newfmt, which I believe avoids at least some of the problems discussed in #17746 and solves #17784 completely.

4ad commented 7 years ago

My first preference is for a type-only type NewFoo = old.Foo.

If a more general solution is desired, I agree with @cznic that a dedicated keyword is better than a new operator (especially an asymetric operator with confusing directionality[1]). That being said, I don't think the export keyword conveys the right meaning. Neither the syntax, nor semantics mirrors import. What about alias?

I understand why @cznic doesn't want the new names to be accesible in the package declaring them, but, to me at least, that restriction feels unexpected and artificial (although I perfectly well understand the reason behind it).

[1] I have been using Unix for almost 20 years, and I still can't create a symlink on the first try. And I usually fail even on the second try, after I have read the manual.

iand commented 7 years ago

I would like to propose an additional constraint: type aliases to standard library types may only be declared in the standard library.

My reasoning is that I don't want to work with code that has renamed standard library concepts to fit a custom naming convention. I also don't want to deal with long spaghetti chains of aliases across multiple packages that end up back at the standard library.

quentinmit commented 7 years ago

@iand: That constraint would block the use of this feature to migrate anything into the standard library. Case in point, the current migration of Context into the standard library. The old home of Context should become an alias for the Context in the standard library.

iand commented 7 years ago

@quentinmit that is unfortunately true. It also limits the use case for golang.org/x/image/draw in this CL https://go-review.googlesource.com/#/c/32145/

My real concern is with people aliasing things like interface{} and error

joegrasse commented 7 years ago

If it is decided to introduce a new operator, I would like to propose ~. In the English language, it is generally understood to mean "similar to", "approximately", "about", or "around". As @4ad above stated, the => is an asymetric operator with confusing directionality.

For example:

const OldAPI ~ NewPackage.API
func  OldAPI ~ NewPackage.API
var   OldAPI ~ NewPackage.API
type  OldAPI ~ NewPackage.API
jba commented 7 years ago

@iand if we limit the right-hand side to a package-qualified identifier, then that would eliminate your specific concern.

It would also mean you couldn't have aliases to any types in the current package, or to long type expressions like map[string]map[int]interface{}. But those uses have nothing to do with the main goal of gradual code repair, so maybe they are no great loss.

rsc commented 7 years ago

@cznic, @iand, others: Please note that restrictions add complexity. They complicate the explanation of the feature, and they add cognitive load for any user of the feature: if you forget about a restriction, you have to puzzle through why something you thought should work doesn't.

It's often a mistake to implement restrictions on a trial of a design solely due to hypothetical misuse. That happened in the alias proposal discussions, and it made the aliases in the trial unable to handle the io.ByteBuffer => bytes.Buffer conversion from the article. Part of the goal of writing the article is to define some cases we know we want to be able to handle, so that we don't inadvertently restrict them away.

As another example, it would be easy to make a misuse argument to disallow non-pointer receivers, or to disallow methods on non-struct types. If we'd done either of those, you couldn't create enums with String() methods for printing themselves, and you couldn't have http.Headers both be a plain map and provide helper methods. It's often easy to imagine misuses; compelling positive uses can take longer to appear, and it's important to create space for experimentation.

As yet another example, the original design and implementation for pointer vs value methods did not distinguish between the method sets on T and *T: if you had a *T, you could call the value methods (receiver T), and if you had a T, you could call the pointer methods (receiver *T). This was simple, with no restrictions to explain. But then actual experience showed us that allowing pointer method calls on values led to a specific class of confusing, surprising bugs. For example, you could write:

var buf bytes.Buffer
io.Copy(buf, reader)

and io.Copy would succeed, but buf would have nothing in it. We had to choose between explaining why that program ran incorrectly or explaining why that program didn't compile. Either way there were going to be questions, but we came down on the side of avoiding incorrect execution. Even so, we still had to write a FAQ entry about why the design has a hole cut out of it.

Again, please remember that restrictions add complexity. Like all complexity, restrictions need significant justification. At this stage in the design process it is good to think about restrictions that might be appropriate for a particular design, but we should probably only implement those restrictions after actual experience with the unrestricted, simpler design helps us understand whether the restriction would bring enough benefits to pay for its cost.

rsc commented 7 years ago

Also, my hope is that we can reach a tentative decision about what to try and then have something ready for experimentation at the beginning of the Go 1.9 cycle (ideally the day the cycle opens). Having more time to experiment will have many benefits, among them an opportunity to learn whether a particular restriction is compelling. One mistake with alias was not committing a complete implementation until near the end of the Go 1.8 cycle.

btracey commented 7 years ago

One thing about the original alias proposal is that in the intended use case (enabling refactoring) the actual use of the alias type should only be temporary. In the protobuffer example, the io.BytesBuffer stub was deleted once the gradual repair had been concluded.

If the alias mechanism should only be seen temporarily, does it actually require a language change? Perhaps instead there could be a mechanism to supply gc with a list of "aliases". gc could temporarily make the substitutions, and the author of the downstream codebase could gradually remove items in this file as fixes are merged. I realize this suggestion also has tricky consequences, but it at least encourages a temporary mechanism.

Merovius commented 7 years ago

I will not participate in the bikeshedding about syntax (I basically don't care), with one exception: If adding aliases is decided and if it's decided to restrict them to types, please use a syntax that is consistently extensible to at least var, if not also func and const (all proposed syntactical constructs allow for all, except type Foo = pkg.Bar). The reason is that, while I agree that cases where aliases for var make the difference might be rare, I don't think they are non-existent and as such believe that we might well at some point decide to add them too. At that point we definitely will want to have all alias declarations be consistent, it would be bad if it's type Foo = pkg.Bar and var Foo => pkg.Bar.

I'd also slightly argue for having all four. The reasons are

1) there is a distinction for var and I do sometimes use it. For example I often expose a global var Debug *log.Logger, or reassign global singletons like http.DefaultServeMux to intercept/remove registrations of packages that add handlers to it.

2) I also think that, while func Foo() { pkg.Bar() } does the same thing as func Foo => pkg.Bar, the intention of the latter is much clearer (especially if you already know about aliases). It clearly states "this isn't really meant to be here". So while technically identical, the alias syntax might serve as documentation.

It's not the hill I'd die on, though; type-aliases alone for now would be fine with me, as long as there is the option to extend them later.

I'm also super glad that this was written up like it was. It summarizes a bunch of opinions I had about API design and stability for a while and will, in the future, serve as a simple reference to link people too :)

However, I also want to emphasize that there where additional use cases covered by aliases that are different from the doc (and AIUI the more general intention of this issue, which is to find some solution to solve gradual repair). I am very glad if the community can agree on the concept of enabling gradual repair, but if a different decision from aliases is decided to reach it, I'd also think that in that case there should be simultaneously talk about if and how to support things like the protobuf public imports or the x/image/draw use case of drop-in replacement packages (both somewhat near to my heart too) with a different solution. @btracey's proposal of a go-tool/gc flag for aliases is an example where I believe that, while it covers gradual repair relatively well, it is not really acceptable for those other usecases. You can't really expect everyone who wants to compile something that uses x/image/draw to pass those flags, they should just be able to go get.

bcmills commented 7 years ago

@jba

@iand if we limit the right-hand side to a package-qualified identifier, then that would eliminate your specific concern.

It would also mean you couldn't have aliases to any types in the current package, […]. But those uses have nothing to do with the main goal of gradual code repair, so maybe they are no great loss.

Renaming within a package (e.g. to a more idiomatic or consistent name) is certainly a type of refactoring one might reasonably want to do, and if the package is used widely then that necessitates gradual repair.

I think a restriction to only package-qualified names would be a mistake. (A restriction to only exported names might be more tolerable.)

bcmills commented 7 years ago

@btracey

Perhaps instead there could be a mechanism to supply gc with a list of "aliases". gc could temporarily make the substitutions, and the author of the downstream codebase could gradually remove items in this file as fixes are merged.

A mechanism for gc would either mean that the code is only buildable with gc during the repair process, or that the mechanism would have to be supported by the other compilers (e.g. gccgo and llgo) too. A "non-language-change" mechanism which must be supported by all implementations is a de facto language change — it's just one with poorer documentation.

rsc commented 7 years ago

@btracey and @bcmills, and not just the compilers: any tool that analyzes source code, like guru or anything else people have built. It's certainly a language change no matter how you slice it.

btracey commented 7 years ago

Okay, thanks.

jimmyfrasche commented 7 years ago

Another possibility is aliases for everything except consts (and @rsc please forgive me for proposing a restriction!)

For consts, => is really just a longer way to write =. There's no new semantics, as with types and vars. There's no saved keystrokes as with funcs.

That would resolve #17784 at least.

The counterargument would be that tooling could treat the cases differently and that it could be an indicator of intent. That's a good counterargument, but I don't think it outweighs the fact that it's basically two ways to do exactly the same thing.

That said, I'm fine with just type aliases for now, they are certainly the most important. I definitely agree with @Merovius that we should strongly consider retaining the option for adding var and func aliases in the future, even if those doesn't happen for some time.

xiegeo commented 7 years ago

How about hiding aliases behind an import, just like for "C" and “unsafe”, to further discourage it's usage? In the same vein, I would like the aliases syntax to be verbose and stand out as a scaffold for on going refactoring.

josharian commented 7 years ago

As an attempt to open up the design space a little, here are some ideas. They're not fleshed out. They're probably bad and/or impossible; the hope is mainly to trigger new/better ideas in others. And if there's any interest, we can explore further.

The motivating idea for (1) and (2) is to somehow use conversion instead of aliases. In #17746, aliases ran into issues around having multiple names for the same type (or multiple ways to spell the same name, depending on whether you think of aliases as like #define or as like hard links). Using conversion sidesteps that by keeping the types distinct.

  1. Add more automatic conversion.

When you call fmt.Println("abc") or write var e interface{} = "abc", "abc" is automatically converted to an interface{}. We could change the language so that when you have declared type T struct { S }, and T has no non-promoted methods, the compiler will automatically convert between S and T as necessary, including recursively inside other structs. T could then serve as a de-facto alias of S (or vice versa) for gradual refactoring purposes.

  1. Add a new "looks like" kind of type.

Let type T ~S declare a new type T that is a type that "looks like S". More precisely, T is "any type convertible to and from type S". (As always, syntax could be discussed later.) Like interface types, T cannot have methods; to do basically anything at all with T, you need to convert it to S (or a type convertible to/from S). Unlike interface types, there is no "concrete type", conversion between S to T and T to S involves no representation changes. For gradual refactoring, these "looks like" types would allow authors to write APIs accepting both old and new types. ("Looks like" types are basically a highly restricted, simplified union type.)

  1. Type tags

Bonus super-hideous idea. (Please don't bother telling me this is awful--I know it. I'm only trying to spur new ideas in others.) What if we introduced type tags (like struct tags), and used special type tags to set up and control aliases, like say type T S "alias:\"T\"". Type tags will have other uses as well and it provides scope for more specification of aliases by the package author than merely "this type is an alias"; for example, the author of the code could specify embedding behavior.

nigeltao commented 7 years ago

If we do try aliases again, it might be worth thinking about "what does godoc do", similar to the "what does iota do" and "what does embedding do" issues.

Specifically, if we have

type  OldAPI => NewPackage.API

and NewPackage.API has a doc comment, are we expected to copy/paste that comment next to "type OldAPI", are we expected to leave it un-commented (with godoc automatically providing a link or automatically copy/pasting), or will there be some other convention?

nigeltao commented 7 years ago

Somewhat tangential, while the primary motivation is and should be supporting gradual code repair, a minor use case (going back to the alias proposal, since that is a concrete proposal) could be to avoid a double function-call overhead when presenting a single function backed by multiple, build-tag-dependent implementations. I'm only hand-waving right now, but I feel like aliases could have been useful in the recent https://groups.google.com/d/topic/golang-nuts/wb5I2tjrwoc/discussion "Avoiding function call overhead in packages with go+asm implementations" discussion.

jimmyfrasche commented 7 years ago

@nigeltao re godoc, I think:

It should always link to the original, regardless.

If there's docs on the alias, those should be displayed, regardless.

If there are not docs on the alias, it's tempting to have godoc display the original docs, but the name of the type would be wrong if the alias also changed the name, the docs could refer to items not in the current package, and, if it's being used for gradual refactoring, there could be a message that says "Deprecated: use X" when you're looking at X.

However, maybe that wouldn't matter for the majority of use cases. Those are things that could go wrong, not things that will go wrong. And some of them could be detected by linting, like renamed aliases and accidentally copying deprecation warnings.

tux21b commented 7 years ago

I am not sure if the following idea had been posted before, but what's about a mostly tool-based "gofix" / "gorename" like approach? To elaborate:

The last steps might complicate / slow-down the compiler a bit, but it's basically just a pre-processor and the amount of rewrite rules should be kept small anyway. So, enough brainstorming for today :)

uluyol commented 7 years ago

Using aliases to avoid function call overhead seems like a hack to work around the compiler's inability to inline non-leaf functions. I don't think implementation deficiencies should influence the language spec.

Merovius commented 7 years ago

@josharian While you didn't intend them as full proposals, let me response (even if only, so that whoever is inspired by you can take the immediate criticism into account):

  1. Doesn't really solve the problem, because conversions aren't really the issue. x/net/context.Context is assignable/convertable/whateverable to context.Context. The problem are higher-order types; namely the types func (ctx x/net/context.Context) and func (ctx context.Context) are not the same, even though the arguments are assignable. So, for 1 to solve the problem, type T struct { S } would need to mean, that T and S are identical types. Which means, that you are simply using a different syntax for aliases after all (just that this syntax already has a different meaning).

  2. Again has a problem with higher-order types, because assignable/convertible types do not necessarily have the same memory representation (and if they do, the interpretation might change significantly). For example, an uint8 is convertible to an uint64 and vice-versa. But that would mean, that, e.g. with type T ~uint8, the compiler can't know how to call a func(T); does it need to push 1, 2,4 or 8 bytes on the stack? There might be ways around this issue, but it sounds pretty complicated to me (and harder to understand than aliases).

josharian commented 7 years ago

Thanks, @Merovius.

  1. Yes, I missed interface satisfaction here. You're right, this doesn't do the job.

  2. I had in mind "have the same memory representation". Convertible back-and-forth is clearly not the right elucidation of that--thanks.

nigeltao commented 7 years ago

@uluyol yes, it's largely about the compiler's inability to inline non-leaf functions, but explicit aliasing might be less surprising with respect to whether or not inlined calls to non-leafs should show up in stack traces, runtime.Callers, etc.

In any case, as I said, it's a minor tangent.

Merovius commented 7 years ago

@josharian Similar problem: [2]uintptr and interface{} have the same memory representation; so only relying on memory representation will allow circumventing type safety. uint64 and float64 have both the same memory representation and are convertible back-and-forth, but would still lead to really weird results at the least, if you don't know which is which.

You might get away with "same underlying type", though. Not sure what the implications would be for that. Off the top of my hat, that might lead to wrongness if a type is used in fields, for example. If you have type S1 struct { T1 } and type S2 struct { T2 } (with T1 and T2 the same underlying type), then under type L1 ~T1 both might be work as type S struct { L1 }, but as T1 and T2 still have a different (though looking alike) underlying type, with type L2 ~S1 you won't have S2 looking alike S1 and not be usable as an L2.

So you'd have to, in a bunch of places in the spec, replace or amend "identical types" with "same underlying type" to make this work, which seems unwieldy and will probably have unforeseen consequences for type safety. "look-alike" types also seem to have an even greater abuse and confusion potential than aliases, IMHO, which seem to be the main arguments against aliases.

If anyone can come up with a simple rule for it, though, that doesn't have these problems, it should definitely be considered as an alternative :)

iand commented 7 years ago

Following on from @josharian's ideation, here's a variation of his number 2:

Allow the specification of "substitutable types". This is a list of types that may be substituted for the named type in function arguments, return values etc. The compiler would allow calling of a function with an argument of the named type or any of its substitutes. The substitute types must have a compatible definition with the named type. Compatible here means identical memory representations and identical declarations after allowing for other substitute types in the declaration.

One immediate problem is that the directionality of this relationship is opposite to the alias proposal which inverts the dependency graph. This alone might make it unworkable but I propose it here because others might think of a way around this. One way might be to declare substitutes as //go comments rather than via the import graph. In this way they perhaps become more like macros.

Conversely there are some advantages to this reversal of directionality:

Applying this to the Context refactoring: the standard library context package would declare that context.Context may be substituted by golang.org/x/net/context.Context. This means any usage that accepts context.Context may also accept a golang.org/x/net/context.Context in its place. However functions in the context package that return a Context would always return a context.Context.

This proposal circumvents the embedding issue (#17746) because the name of the embedded type never changes. However, an embedded type could be initialised using a value of a substitute type.

4ad commented 7 years ago

@iand @josharian you are asking for a certain variant of covariant types.

rsc commented 7 years ago

@josharian, thanks for the suggestions.

Re type T struct { S }, that looks like a different syntax for alias, and not necessarily a clearer one.

Re type T ~S, I am either not sure how it differs from alias or not sure how it helps refactoring. I guess in a refactoring (say, io.ByteBuffer -> bytes.Buffer), you would write:

package io
type ByteBuffer ~bytes.Buffer

but then if, as you say, "to do basically anything at all with T, you need to convert it to S", then all the code doing anything with io.ByteBuffer still breaks.

Re type T S "alias": A key point @bcmills made above is that having multiple equivalent names for types is a language change, no matter how it is spelled. All compilers need to know that, say, io.ByteBuffer and bytes.Buffer are the same, as do any tools that analyze or even type-check code. The key part of your suggestion seems to me something like "maybe we should plan ahead for other additions". Maybe, but it's unclear that a string would be the best way to describe those, and it's also unclear we want to design syntax (like Java generalized annotations) without a clear need. Even if we did have a general form, we'd still need to consider carefully all the implications of any new semantics we introduced, and most would still be language changes that would require updating all tools (except gofmt, admittedly). On balance it seems simpler to continue to find the clearest way to write the forms we need one by one instead of creating a meta-language of one kind or another.

@Merovius FWIW, I would say that [2]uintptr and interface{} do not have the same memory representation. An interface{} is a [2]unsafe.Pointer not a [2]uintptr. A uintptr and a pointer are different representations. But I think your general point is right, that we do not want to necessarily allow direct conversion of that kind of thing. I mean, can you convert from interface{} to [2]*byte too? It's a lot more than is needed here.

rsc commented 7 years ago

@jimmyfrasche and @nigeltao, re godoc: I agree that we need that working early too. I agree that we should not hard-code the assumption "the new feature - whatever it is - will only be used for codebase refactoring". It may have other important uses, like Nigel found for helping to write a draw extension package with aliases. I expect that deprecated things will be marked deprecated in their doc comments explicitly, as Jimmy said. I did think about generating a doc comment automatically if one is not there, but there's nothing obvious to say that shouldn't already be clear from the syntax (speaking generally). To make a specific example, consider the old Go 1.8 aliases. Given

type ByteBuffer => bytes.Buffer

we could synthesize a doc comment saying "ByteBuffer is an alias for bytes.Buffer", but that seems redundant with displaying the definition. If someone writes "type X struct{}" today, we don't synthesize "X is a named type for a struct{}".

rsc commented 7 years ago

@iand, thanks. It sounds like your proposal requires the author of the new package to write the exact definition from the old package and then also a declaration linking the two, like (making up syntax):

package old
type T { x int }

package new
import "old"
type T1 { x int }
substitutable T1 <- old.T

I agree that the import reversal is problematic and may be a show-stopper by itself, but let's skip that. At this point the codebase seems like it is in a fragile state: now package new can be broken by a change to add a struct field in package old. Given the substitutable line, there is only one possible definition for T1: exactly the same as old.T. If the two types still have distinct definitions then you also have to worry about the methods: do the method implementations need to match too? If not, what happens when you put a T in an interface{} and then pull it out using a type assertion as a T1 and call M()? Do you get T1.M? What if you pull it out as an interface { M() }, without naming T1 directly, and call M()? Do you get T.M? There's a lot of complexity caused by the ambiguity of having both definitions in the source tree.

Of course, you could say that the substitutable line makes the rest redundant and not require a definition for type T1 or any methods. But then that's basically the same as writing (in the old alias syntax) type T1 => old.T.

Getting back to the import graph issue, although the examples in the article all made the old code defined in terms of the new code, if the package graph were such that new had to import old instead, it's equally effective to put the redirect in the new package during the transition.

I think this shows that in any transition like this, there's probably not a useful distinction between the author of the new package and the author of the old package. By the end, the goal is that code has been added to new and deleted from old, so both authors (if they're different) need to be involved then. And the two need some kind of coordinated compatibility during the middle too, whether explicit (some kind of redirect) or implicit (type definitions must match exactly, as in the substitutability requirement).

iand commented 7 years ago

@rsc that breakage scenario suggests that any type aliasing needs to be bidirectional. Even under the previous alias proposal any change in new package could potentially break any number of packages that happen to have aliased the type.

rsc commented 7 years ago

@iand If there's only one definition (because the other says "same as that one") then there's no worry about them not being in sync.

ianlancetaylor commented 7 years ago

Over in #13467, @joegrasse points out that it would be nice if this proposal provided a mechanism for permitting identical C types to become identical Go types when using cgo in multiple packages. That is not at all the same problem as the one that this issue is for, but both problems are related to type aliasing.

thwd commented 7 years ago

Is there any summary of proposed/accepted/rejected restrictions/limitations on aliases? Some questions that pop to mind are:

iand commented 7 years ago

@rsc I don't want to divert the conversation too much but under the alias proposal if "new" removes a field that "old" relied on it means clients of "old" now can't compile.

However, under the substitute proposal I think it could be arranged that only clients that use both old and new together would break. For that to be possible then the substitution directive would have to validated only when the compiler detects a use of "old" types in "new" package.

Merovius commented 7 years ago

@thwd I don't think there is a good writeup yet. My notes:

rsc commented 7 years ago

@iand, re "only clients that use both old and new together would break", that's the only interesting case. It's the mixed clients that make it a gradual code repair. Clients that use only the new code or only the old code will work today.

iainmerrick commented 7 years ago

There's something else to consider, that I haven't seen mentioned elsewhere yet:

Since an explicit goal here is to allow large, gradual refactoring in large decentralized codebases, there'll be situations where a library owner wants to do some kind of cleanup that will require an unknown number of clients to change their code (in the final "retire the old API" step). A common way to do that is to add a deprecation warning, but the Go compiler doesn't have any warnings.

Without any kind of compiler warning, how can a library owner be confident that it's safe to complete the refactoring?

One answer might be some kind of versioning scheme — it's a new release of the library with a new incompatible API. In that case maybe versioning is the whole answer, not type aliases.

Alternatively, how about allowing the library author to add a "deprecation warning" that actually causes a compile error for clients, but with an explicit algorithm for the refactoring that they need to perform? I'm imagining something like:

Error: os.time is obsolete, use time.time instead. Run "go upgrade" to fix this.

For type aliases, I guess the refactoring algorithm would just be "replace all instances of OldType with NewType", but there might be subtleties, I'm not sure.

Anyway, that would allow the library author to make a best-effort attempt to warn all clients that their code is about to break, and give them an easy way to fix it, before deleting the old API completely.

zombiezen commented 7 years ago

@iainmerrick There are bugs open for these: golang/lint#238 and golang/gddo#456

jimmyfrasche commented 7 years ago

Solving the gradual code repair problem, as outlined in @rsc's article, reduces to requiring a way for two types to be interchangeable (as workarounds exist for vars, funcs, and consts).

This needs either a tool or a change to the language.

Since making two types interchangeable is, by definition, changing how the language works, any tool would be a mechanism for simulating the equivalence outside of the compiler, likely by rewriting all the instances of the old type to the new type. But this means such a tool would have to rewrite code you don't own, like a vendored package that uses golang.org/x/net/context instead of the stdlib context package. The specification for the change would either have to be in a separate manifest file or a machine-readable comment. If you don't run the tool you get build errors. That all gets messy to deal with. It seems like a tool would create as many problems as it solves. It'd still be a problem everyone using these packages has to deal with, albeit somewhat nicer since a portion is automated.

If the language is changed, code only needs to be modified by its maintainers, and, for most people, things just work. Tooling to aid the maintainers is still an option, but it'd be much simpler since the source is the specification, and only the maintainers of a package would need to invoke it.

As @griesemer pointed out (I don't recall where, there have been so many threads about this) Go already has aliasing, for stuff like byteuint8, and when you import a package twice, with different local names, into the same source file.

Adding a way to explicitly alias types in the language is just allowing us to use semantics that already exist. Doing so solves a real problem in a manageable way.

A language change is still a big deal and a lot of things need to be worked out, but I think that it's ultimately the right thing to do here.

akavel commented 7 years ago

As far as I'm aware, one "elephant in the room" is the fact, that for type aliases, introducing them will allow for non-temporary (i.e. "non-refactoring") usages. I've seen those mentioned in passing (for example, "reexporting type identifiers in different package to simplify API"). Keeping up with good tradition of previous proposals, please list all the known alternative usages of type aliases under "impact" subsection. This should also bring the benefit of fueling people's imagination for inventing additional possible alternative usages and bringing them into light in the current discussion. As is now, the proposal appears to pretend that authors are completely unaware of other possible uses of type aliases. Also, as to reexporting, Rust/OCaml may have some experience with how those work for them.

Additional question: please clarify if type aliases would allow adding methods to the type in the new package (arguably breaking encapsulation) or not? also, would the new package get access to private fields of old structs, or not?

jba commented 7 years ago

Additional question: please clarify if type aliases would allow adding methods to the type in the new package (arguably breaking encapsulation) or not? also, would the new package get access to private fields of old structs, or not?

An alias is just another name for a type. It doesn't change the type's package. So no to both of your questions (unless new package == old package).

rsc commented 7 years ago

@akavel As of now, there is no proposal at all. But we do know of two interesting possibilities that came up during the Go 1.8 alias trials.

  1. Aliases (or just type aliases) would enable creating drop-in replacements that expand other packages. For example see https://go-review.googlesource.com/#/c/32145/, especially the explanation in the commit message.

  2. Aliases (or just type aliases) would enable structuring a package with a small API surface but a large implementation as a collection of packages for better internal structure but still present just one package to be imported and used by clients. There's a somewhat abstract example described at https://github.com/golang/go/issues/16339#issuecomment-232813695.

niemeyer commented 7 years ago

The underlying goal of aliases is great, but it still sounds like we're not being quite honest to the goal of refactoring code, despite it being the number one motivator for the feature. Some of the proposals suggest locking down the name, and I haven't seen it mentioned yet that types usually change their surface with such refactorings too. Even the example of os.Error => error often mentioned around aliases ignores the fact that os.Error had a String method and not Error. If we just moved the type and renamed it, all error handling code would be broken regardless. That's common place during refactorings.. old methods get renamed, moved, dropped, and we don't want them in the new type as it would preserve the incompatibility with new code.

In the interest of helping out, here is a seed idea: what if we looked at the problem in terms of adapters, instead of aliases? An adapter would give an existing type an alternative name and interface, and it can be used unadorned in places where the original type was seen before. The adapter would need to explicitly define the methods it supports, rather than assuming the same interface of the underlying adapted type are present. This would be much like the existing type foo bar behavior, but with some additional semantics.

io.ByteBuffer

For instance, here is an example skeleton addressing the io.ByteBuffer case, using the temporary "adapts" keyword for the time being:

type ByteBuffer adapts bytes.Buffer

func (old *ByteBuffer) Write(b []byte) (n int, err error) {
        buf := (*bytes.Buffer)(old)
        return buf.Write(b)
}

(... etc ...)

So, with that adapter in place, this code would all be valid:

func newfunc(b *bytes.Buffer) { ... }
func oldfunc(b *io.ByteBuffer) { ... }

func main() {
        var newvar bytes.Buffer
        var oldvar io.BytesBuffer

        // New code using the new type obviously just works.
        newfunc(&newvar)

        // New code using the old type receive the underlying value that was adapted.
        newfunc(&oldvar)

        // Old code using the old type receive the adapted value unchanged.
        oldfunc(&oldvar)

        // Old code gets new variable adapted on the way in. 
        oldfunc(&newvar)
}

The interfaces of newfunc and oldfunc are compatible. Both actually accept *bytes.Buffer, with oldfunc adapting it to *io.BytesBuffer on the way in. The same concept works for assignments, results, etc.

os.Error

The same logic probably be made to work on interface too, although the compiler implementation of it is a bit trickier. Here is an example for os.Error => error, which handles the fact the method was renamed:

package os

type Error adapts error

func (e Error) String() string { return error(e).Error() }

This case needs further thinking, though, because methods such as:

func (v *T) Read(b []byte) (int, os.Error) { ... }`

Will be returning a type that has a String method, so we'd usually want to adapt in the opposite direction so code can be gradually fixed.

UPDATED: Needs further thinking.

Embedding problem

In terms of the embedding bug that dragged the feature out of 1.8, the outcome is a bit more clear with adapters, since they're not just new names for the same thing: if the adapter is embedded, the field name used is that of the adapter so old logic remains working, and accessing the field will use the adapter interface unless explicitly handed into a context that takes the underlying type. If the unadapted type is embedded, the usual happens.

kubernetes, docker

The problems stated in the post seem like variations of the above issues, and solved by the proposal.

vars, consts

It wouldn't make much sense to adapt variables or constants under that scenario, since we can't really associate methods with them directly. It's their types that would be adapted or not.

godoc

We'd be explicit about the fact the thing is an adapter, and show the documentation for it as usual, since it contains an independent interface from the adapted thing.

syntax

Please pick something nice. ;)