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

Closed
rsc opened this Issue Dec 1, 2016 · 225 comments

Comments

Projects
None yet
Contributor

rsc commented Dec 1, 2016 edited

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 (#16339 (comment)) and @ianlancetaylor (#16339 (comment)) 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 (#18130 (comment) 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, ~ (#18130 (comment) by @joegrasse), or export (#18130 (comment) 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 #18130 (comment) 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 (#18130 (comment)) 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, #18130 (comment) by @btracey).

Or maybe if the compiler can apply some kind of rule-based preprocessing to transform input files before compilation (for example, #18130 (comment) 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 (#18130 (comment)), “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 #16339 (comment).

  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. (#18130 (comment) by @nigeltao and #18130 (comment) by @jimmyfrasche)

  2. Can methods be defined on types named by alias? (#18130 (comment) by @ulikunitz)

  3. If aliases to aliases are allowed, how do we handle alias cycles? (#18130 (comment) by @thwd)

  4. Should aliases be able to export unexported identifiers? (#18130 (comment) by @thwd)

  5. What happens when you embed an alias (how do you access the embedded field)? (#18130 (comment) by @thwd, also #17746)

  6. Are aliases available as symbols in the built program? (#18130 (comment) by @thwd)

  7. Ldflags string injection: what if we refer to an alias? (#18130 (comment) 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."
(#18130 (comment) 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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @atdiar raises the question of the effect on object files and separate compilation. In #18130 (comment), @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 #18130 (comment), @iand suggests "substitutable types", "a list of types that may be substituted for the named type in function arguments, return values etc.". In #18130 (comment), @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 #18130 (comment), @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 #18130 (comment), @rsc takes the theoretical way, expanding on #18130 (comment) 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 #18130 (comment), @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 #18130 (comment) by @Merovius and #18130 (comment) by @zombiezen).

Restrictions

This section collects proposed restrictions for reference, but keep in mind that restrictions add complexity. As I wrote in #18130 (comment), "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.

(#18130 (comment) and #18130 (comment) 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.

(#18130 (comment) 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 (#18130 (comment) 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." - #18130 (comment) 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." - #18130 (comment) 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 #18130 (comment) 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." - #18130 (comment) 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.

rsc added the Proposal label Dec 1, 2016

rsc added this to the Proposal milestone Dec 1, 2016

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 Dec 1, 2016 edited

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 Dec 1, 2016

As Russ's article points out, any alias-like solution needs to gracefully solve #17746 and #17784

travisjeffery commented Dec 1, 2016 edited

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:

  • It's simpler.
    The alternative solution => having subtly different meaning based on its operand feels out of place for Go.
  • It's focused and conservative.
    The issue at hand with types is solved and you don't need to worry about imagining the complications of the generalized solution.
  • It's aesthetic.
    I think it looks more pleasing.

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.

Contributor

cznic commented Dec 1, 2016

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.

Member

4ad commented Dec 1, 2016 edited

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 Dec 1, 2016

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.

Contributor

quentinmit commented Dec 1, 2016

@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 Dec 1, 2016

@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

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 Dec 1, 2016

@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.

Contributor

rsc commented Dec 1, 2016 edited

@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.

Contributor

rsc commented Dec 1, 2016

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.

Contributor

btracey commented Dec 1, 2016

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 Dec 1, 2016

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.

Member

bcmills commented Dec 1, 2016

@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.)

Member

bcmills commented Dec 1, 2016

@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.

Contributor

rsc commented Dec 1, 2016

@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.

Contributor

btracey commented Dec 1, 2016

Okay, thanks.

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 Dec 1, 2016 edited

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.

Contributor

josharian commented Dec 1, 2016

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.

Contributor

nigeltao commented Dec 1, 2016

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?

Contributor

nigeltao commented Dec 1, 2016

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.

@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.

Contributor

tux21b commented Dec 1, 2016

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:

  • any package can contain a set of rewriting rules (e.g. mapping pkg.Ident => otherpkg.Ident)
  • those rewriting rules can be specified with //+rewrite ... tags inside arbitrary go files
  • those rewriting rules are not limited to ABI compatible changes, it's also possible to do other things (e.g. pkg.MyFunc(a) => pkg.MyFunc(context.Contex(), a))
  • a gofix like tool can be used to apply all transformations to the current repository. This makes it easy for users of a package to update their code.
  • it's not necessary to call the gofix tool in order to compile successfully. A library that still wants to use the old API of a dependency X (to stay compatible with old and new versions of X) can still do so. The go build command should apply the transformations (specified in the rewrite tags of package X) on-the-fly without changing the files on disk.

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 :)

Contributor

uluyol commented Dec 1, 2016

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 Dec 1, 2016

@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).

Contributor

josharian commented Dec 1, 2016

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.

Contributor

nigeltao commented Dec 2, 2016

@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 Dec 2, 2016

@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 Dec 2, 2016

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:

  • the set of substitutable types is controlled by the author of the new package who is better placed to guarantee semantics
  • no code changes are required in the original package so clients don't have to update until they start using the new package

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.

Member

4ad commented Dec 2, 2016 edited

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

Contributor

rsc commented Dec 2, 2016

@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.

Contributor

rsc commented Dec 2, 2016

@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{}".

Contributor

rsc commented Dec 2, 2016

@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 Dec 2, 2016

@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.

Contributor

rsc commented Dec 2, 2016

@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.

Contributor

ianlancetaylor commented Dec 2, 2016

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 Dec 2, 2016 edited

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

  • Is the RHS always fully-qualified?
  • If aliases to aliases are allowed, how do we handle alias cycles?
  • Should aliases be able to export unexported identifiers?
  • What happens when you embed an alias? (how do you access the embedded field)
  • Are aliases available as symbols in the built program?
  • ldflags string injection: what if we refer to an alias?

iand commented Dec 2, 2016

@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 Dec 2, 2016

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

  • Alias cycles are a non-issue. In case of package-crossing aliases, a cycle is already disallowed because of an import-cycle. In case of non package-crossing aliases, they obviously need to be disallowed, which is very similar to cycles in initialization order. Personally, I'd like to have aliases to aliases, because I don't think they should be restricted to gradual repair use cases (see my comment above) and it would be sad, if package A could break by someone moving a type in package B with an alias (imagine x/image/draw.Image aliasing draw.Image and then someone deciding to move draw.Image into image.Draw via an alias, assuming it safe. Suddenly x/image/draw breaks, because aliases to aliases are not allowed).
  • I think previously proponents of aliases have agreed that alias-exporting unexported identifiers is probably a bad idea due to the weirdness it may cause. Effectively that means that aliases to unexported identifiers are useless and may be disallowed completely.
  • The embedding question, AFAIK, is as of yet unsolved. There is a whole discussion in #17746, I expect that this discussion will be continued if/when/before it is decided to go forward with aliases (but there's still the possibility of an alternative solution or the decision to not make gradual repairs a goal at all)
Contributor

rsc commented Dec 2, 2016

@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.

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.

Member

zombiezen commented Dec 2, 2016

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

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.

Contributor

akavel commented Dec 3, 2016

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 Dec 4, 2016

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).

Contributor

rsc commented Dec 5, 2016

@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 #16339 (comment).

Contributor

niemeyer commented Dec 6, 2016 edited

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. ;)

Member

rakyll commented Dec 6, 2016

@iainmerrick @zombiezen

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.

Contributor

ulikunitz commented Dec 6, 2016 edited

Two observations:

1. Semantics of type references depend on supported refactoring use case

Gustavo's proposal shows that there needs to more work on the use case for type references and the resulting semantics.

Ross' new proposal includes a new syntax type OldAPI = newpkg.newAPI. But what are the semantics? Is it impossible to extend OldAPI with legacy public methods or fields? Assuming yes as an answer that requires the newAPI to support all public methods and fields of OldAPI to maintain compatibility. Please note that any code in the package with OldAPI that relies on private fields and methods must be rewritten to use only the public newAPI assuming that modifying the visibility constraints of packages is off the table.

The alternate path would be to allow additional methods to be defined for OldAPI. That could ease the burden on NewAPI to provide all public old methods. But that would make OldAPI a different type than NewAPI. Some form of assignability between values of the two types has to be maintained, but rules would become complex. Allowing the addition of fields would result in more complexity.

2. Package with NewAPI cannot import package with OldAPI

Redefinition of the OldAPI requires that package O containing the definition of OldAPI imports package N with the NewAPI. That implies that package N cannot import O. Maybe it is so obvious that it hasn't been mentioned, but it seems to me an important constraint for the refactoring use case.

Update: Package N cannot have any dependency on package O. For example it cannot import a package that imports O.

Merovius commented Dec 6, 2016

@niemeyer Changes like renaming a method are already gradually possible: a) Add the new method, call the old one under the hood (or vice versa), b) gradually change all users to the new method, c) delete the old method. You can combine that with a type alias. The reason this focuses on type-moving is, that this is the only thing identified, that is not yet possible. All other identified changes are possible, even if they may use several steps (for example changing the set of arguments of a method without renaming it). I believe choosing a fix with less surface area (less things to understand) is preferable.

@rakyll Personally, if I'd consider aliases useful for something non-refactoring (like wrapper-packages, which I find an excellent use case) I'd just use them, deprecation-warnings be damned. I'd be pissed at whoever artificially crippled them and made them confusing for my users, but I wouldn't be discouraged.

I think at some point it needs to be debated whether we actually consider wrapper-packages, protobuf public imports or exposing internal package-APIs such a bad thing (and I don't know how to best debate something this subjective without one side just repeating over and over that they're unreadable and the other saying "no they're not". There isn't a lot of objective argument to be had here, it seems to me).

I at least (obviously) think they're a good thing and I'm also of the opinion that adding a language feature and artificially restricting it to only one use case is a bad thing; an orthogonal, well-designed language allows you to do as much as possible with as little features as possible. You want your features to expand the "spanned vector space of possible programs" as much as possible, so adding a feature which only adds a single point to the space seems weird to me.

glyn commented Dec 6, 2016

I'd like another, slightly different use case to be born in mind as any type alias proposal is developed.

Although the main use case we are discussing in this issue is type replacement, type aliases would also be very useful for weaning a body of code off a dependency on a type.

For example, suppose a type turned out to be "unstable" (i.e. it keeps being changed, perhaps in incompatible ways). Then some its users might want to migrate to a "stable" replacement type. I'm thinking of development on github etc. where the owners of a type and its users do not necessarily work closely together or agree on the objective of stability.

Other examples would be where a single type is the only thing stopping a dependency on a large or problematic package from being deleted, e.g. where a license incompatibility has been discovered.

So the process here would be:

  1. Define the type alias
  2. Change the relevant body of code to use the type alias
  3. Replace the type alias with a type definition.

At the end of this process, there would be two independent types which would be free to evolve in their own directions.

Note that in this use case:

  • there is no option of changing the package containing the original type definition to add a type alias there (since the owners are unlikely to agree to this)
  • the original type is not deprecated (although it might be considered as such in the body of code in the process of being "weaned off" the type).
Contributor

niemeyer commented Dec 6, 2016

@Merovius The moment you delete or rename the old method, you kill every client that was using it, at once. If you're willing to do that, the whole non-trivial exercise of adding a language feature to prevent all-at-once breakage is moot. We might as well say exactly the same thing for moving the code: just rename the type on every call site at once. Done. Both actions are simply atomic renames, which have in common the fact they assume complete access to every line of code in the call sites. This might be the case for Google, but as a maintainer of large open source applications and libraries, it's not the world I live in.

In most cases I find that criticism unfair, since the Go team goes through great lengths to make the project inclusive to external parties, but the moment you assume you have access to every single line of code that is calling a given package, that's a walled garden which doesn't match the context of an open source community. Adding a language-level refactoring feature that only works inside walled gardens would be atypical, to say the least.

Merovius commented Dec 6, 2016

@niemeyer I apparently didn't make myself clear. I wasn't advocating for deleting the old API in any case, I was just pointing out that any workflow that we want to enable with type aliases is already possible with renaming methods (whether at the same time or not). So, no matter what you want to do, for

  1. Add new API, interchangeable with old API
  2. Gradually change consumers over to new API
    3a. Once everything is migrated or the deprecation period runs out, delete the old API
    3b. Provide indefinite stability by keeping both APIs around forever (see e.g. this part of the article)

You seem to be arguing about doing 3a vs. 3b. But what I was pointing out, that 1. is already possible for method-names but not possible for types, which is what this is about.

Though, I now realize that I think I misunderstood you :) You might have been pointing out, that os.Error are different interface-definitions, so the move doesn't really pan out. I think that is true; if you forbid removing APIs, type aliases would not make it possible to rename methods of interface types.

Maybe you can clarify something about your adapter idea for me, though: Wouldn't that also allow to use (for example, in the os.Error case) any fmt.Stringer as an os.Error?

In any case, the adapter idea seems worthwile to develop further, even if I'm slightly sceptical about it. But having a way to gradually refactor interfaces without breaking possible implementers and/or consumers is a good goal.

Contributor

rsc commented Dec 6, 2016

@niemeyer Yes, you bring up a good point about the method name changing in error too. That introduces many complications, and it's not something I'm trying to tackle here. Because only a fraction of code mentioning error/os.Error actually calls the method, the move was the more painful part than the method change. I think we can treat method renamings as an independent problem from changing code location. If the move were happening today and we could do the package reorg seamlessly but be stuck with the old method name, that would still be significant progress. Focusing this issue on code location is meant to try to simplify.

I agree that if there were some general fix that handled both kinds of changes, that would be great. I don't see what that fix is. In particular I don't understand how type switches work with the adapters you described: does the value somehow automatically convert during the type switch? What about reflection? Having only one type with two names avoids many problems that arise with having two types that auto-convert back and forth.

Contributor

niemeyer commented Dec 6, 2016

@rsc Yes, the adapter would consistently automatically convert in every situation, so type switches would be no different. We'd forbid type switches containing both the adapter and its underlying type, as that would be ambiguous. I might be missing something, but can't yet see a problem with reflection, since every code context needs to necessarily either be using the adapted type, or its underlying type, explicitly. Just like today, we can't go into an interface{} without knowing how we got there, if that makes sense.

@Merovius My two comments above address precisely the points you're still making. If you move a type today, you break code that needs fixing. If you rename a method, you break code that needs fixing. If you delete a method, change their arguments, you break code that needs fixing. When refactoring code in any of these cases, the fixes need to be done atomically with the breakage in every call site for things to continue working. Allowing the type to be moved but completely untouched is a very limited case of refactoring, which IMO doesn't justify a language feature.

Contributor

rsc commented Dec 6, 2016

@niemeyer That would handle the concrete types. What about a type assertion for .(interface{String() string}) vs .(interface{Error() string}) or whatever specific pieces of interface changed? Does the check have to consider both possible underlying types somehow?

Merovius commented Dec 6, 2016

@niemeyer No. Renaming a method is possible non-atomically. e.g. to move a method from A.Foo to A.Bar, do

  1. Add method A.Bar as a wrapper around A.Foo
  2. Migrate users to only call A.Bar via arbitrarily many commits
  3. Either delete A.Foo, or don't, depending on if you are willing to enforce a deprecation.

Change functions arguments is possible non-atomically. e.g. to add a parameter x int to a func Foo(), do

  1. Add func FooWithInt(x int) { Foo(); // use x somehow; }
  2. Migrate users to add the parameter via arbitrarily many commits
  3. If you are not willing to enforce a deprecation (or you are not bothered by having the WithInt), you are done. Otherwise modify Foo to func Foo(x int) { FooWithInt(x) }.
  4. Migrate users with s/FooWithInt/Foo/g via arbitrarily many commits.
  5. Delete FooWithInt.

The same works for pretty much all cases except moving types (and, strictly speaking, vars). atomicity isn't required. You either break compatibility when enforcing deprecation, or you don't, but that's completely orthogonal to atomicity. The ability to use two different names to refer to the same thing is, what allows you to sidestep atomicity when doing basically arbitrary changes and you have that ability for all cases except types. Yes, to do an actual move, instead of an amendment, you need to be willing to enforce deprecation (so breaking the build of potentially unknown code, meaning this needs wide and timely announcement). But even if you are not, the ability to augment APIs with a more convenient name or other useful wrapping (see x/image/draw) also depends on the ability to refer to the old thing by the new name and vice-versa.

The difference between moving types today and renaming a function today is, that in the former case, you actually need an atomic change, whereas for the latter, you can make the change gradually, over independent repos and commits. Not as a "I'll make a commit that does s/Foo/Bar/", but there is a process to do it.

Anyway. I don't know where we are, apparently, talking past each other. I find @rsc's document pretty clear to convey my POV and don't really get yours :)

Contributor

niemeyer commented Dec 6, 2016

@rsc I can see two reasonable answers. The simple one that the interface carries the type that went in, adapter or otherwise, and usual semantics apply when interface-asserting. The other one is that the value may be unadapted if it doesn't satisfy the interface but the underlying value does. The former one is simpler and perhaps enough for the refactoring use cases we have in mind, while the latter is perhaps more consistent with the idea that we can type-assert it to the underlying type as well.

@Merovius Sure, renaming a method is possible as long as you don't actually rename it and force call sites to use a new API instead. Likewise, moving a type is possible as long as you don't actually move it, and force call sites to use a new API instead. We've all been doing both of these things for years to preserve old code working.

Merovius commented Dec 6, 2016

@niemeyer But again: For types, you can't even add things in a decent manner. See x/image/draw. And not everyone might have such an absolute view of stability; I, myself, am fine with saying "in 6,12,… months $function,$type,… is going away, be sure that you are migrated away from it at that point" and then just break unmaintained code that doesn't manage to follow that deprecation notice (if someone thinks they need long-term-support for APIs, they surely can find someone to pay to provide that). I'd even claim that most people don't have that absolute view on stability; see the recent push for semantic versions, which only really makes sense if you do want to have the option to break compatibility. And the doc argues very well, how, even in that case, you would still profit from the ability of having gradual repairs and how it can alleviate, if not essentially solve the diamond dependency problem.

You might dismiss most of the use cases of aliases for gradual repairs because your stance on stability is absolute. But I'd claim that for most of the go community, that is different, that there is a want for breakages and a use in making them as smoothly as possible when they do happen.

thwd commented Dec 6, 2016

@niemeyer @rsc @Merovius I have been following your discussion (and the entire discussion) and I'd like to blatantly smack this post right in the middle of it.

The more we iterate over the issue, the closer we get to some form of extended covariance semantics. So, here goes a thought: we already have subtype semantics ("is-a") defined from concrete types to interfaces and among interfaces. My proposal is to make interfaces recursively covariant (according to the current variance rules) down to their methods arguments.

This does not solve the problem for all current packages. But it can solve the problem for all future packages, yet to be written, in that the "moveable parts" of the API can be interfaces (encourages good design, too).

I think we can solve all requirements by (ab)using interfaces in this way. Are we breaking Go 1.0? I dont know but I think we're not.

Contributor

griesemer commented Dec 6, 2016

@thwd I think you need to define more precisely what you mean by "making interfaces recursively covariant". Usually, in subtyping, method arguments need to change in contravariant ways, and the results in covariant ways. Also, from what you're saying this wouldn't solve any existing problem with concrete (non-interface) types.

Merovius commented Dec 6, 2016

@thwd I disagree, that interfaces (even covariant ones) are a good solution to any of these problems (only to very specific instances of it). To make them one, you'd need to make everything in your API an interface (because you never know what you might want to move/change at some point), including vars/consts/funcs/… and I don't think at all, that that is good design (I've seen that in java. It aggravates me). If something is a struct, just make it a struct. Everything else just adds weird syntactic overhead in your package and every reverse dependency for virtually no benefit. It's also the only way to stay sane when you begin; start simple and move to something more general later. A lot of complications in API I've seen so far come from people overthinking API design and planning for way more generality than will ever be needed. And then, in 80% (that number is an obvious lie) of the cases, nothing happens at all, because there is no "clean API design".

(to be clear: I'm not saying that covariant interfaces aren't a good idea. I'm just saying that they are not a good solution to these problems)

Member

zombiezen commented Dec 6, 2016

To add to @Merovius's point, many gradual code repairs that I've seen have taken the form of moving a generally useful non-interface type out of a much larger package. Consider the following:

package foo

type Authority struct {
  Host string
  Port int
}

Over time, the foo package grows and it ends up gaining more responsibility (and code size) than someone who just needs the Authority type really wants. So having a way of creating a fooauthority package that just contains Authority and having existing users of foo.Authority still work is a desirable use-case. Note that any solution that only considers interface types would not help here.

Contributor

niemeyer commented Dec 6, 2016

@Merovius Your last comment has been entirely subjective and is addressing me personally instead of my proposal. This won't end well, so I'll stop that line of discussion here.

thwd commented Dec 6, 2016

@griesemer @Merovius I agree with both of you. To close the loop, then, we can agree that the discussion so far has lead us to some notion of subtypes/covariance. Also, that any implementation of it should incur no runtime indirection. That's kind of what @niemeyer was proposing (if I understood him right). But I'd love to read more ideas. I'll be thinking about the problem, too.

jba commented Dec 6, 2016

@niemeyer There was nothing ad hominem in @Merovius's comments. His claim that "your stance on stability is absolute" is an observation about your position, not you, and is a reasonable inference from some of your statements, like

The moment you delete or rename the old method, you kill every client that was using it, at once.

and

Sure, renaming a method is possible as long as you don't actually rename it and force call sites to use a new API instead. Likewise, moving a type is possible as long as you don't actually move it, and force call sites to use a new API instead. We've all been doing both of these things for years to preserve old code working.

I got the same impression as Merovius from those statements—that you aren't sympathetic to deprecating something for a while, then eventually removing it; that you are committed to keeping code in the wild working indefinitely; that "your stance on stability is absolute". (And to forestall further misunderstanding, I'm using "you" to refer to your ideas, not your personality.)

Member

bcmills commented Dec 6, 2016 edited

@niemeyer The adapts declaration you're suggesting seems closely related to instance from Haskell typeclasses. Loosely translating that to Go, it might look something like:

package os

type Error interface {
  String() string
}

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

Unfortunately (as @zombiezen notes), it's not clear how this would help for non-interface types.

It's also not obvious to me how it would interact with function types (arguments and return-values); for example, how would the semantics of adapts help with migrating Context to the standard library?

Contributor

niemeyer commented Dec 6, 2016

I got the same impression as Merovius from those statements—that you aren't sympathetic to deprecating something for a while

@jba These are absolute facts, not an absolute opinions. If you delete a method or a type, Go code using it breaks, so these changes need to be done atomically. My proposal, though, is about gradual refactoring of code, which is the subject here and implies deprecation. That process of deprecation, though, isn't a matter of sympathy. I've got multiple public Go packages with thousands of in-the-wild dependencies each, and multiple independent APIs due to that gradual evolution. When we break an API, it's good to do such breakages in batches, instead of streaming them, if we expect to not get people insane. Unless, of course, you live in a walled garden and can reach out to every call site to fix it. But I'm repeating myself.. all of that can be read in the original proposal above in a more articulated way.

Member

rakyll commented Dec 6, 2016 edited

@Merovius

Personally, if I'd consider aliases useful for something non-refactoring (like wrapper-packages, which I find an excellent use case) I'd just use them, deprecation-warnings be damned.

We maintain packages with wildly large number of new and deprecated APIs and having aliases without clear explanation of the state of the old (aliased) type is not going to help the gradual code repair and will only contribute to the overwhelmingness of the increased API surface. I agree with @niemeyer that our solution needs to address the requirements of a distributed developer community which currently doesn't have any other signals than free-form godoc text saying that an API is "deprecated". Adding a language feature to help deprecating old types is the topic of this thread, hence it naturally leads to question of what's the state of the old (aliased) type.

I would love to discuss type aliasing under a different theme such as providing extension to a type or partial packages but not on this thread. That topic itself has various encapsulation-specific problems to be addressed before any consideration.

A specific operator or implying that the aliased typed is somewhat replaced could be healthy to communicate to the users that they need to switch. Such differentiability would enable tools to automatically report the replaced APIs.

To be clear, deprecation policy is not technically possible for types outside of the standard library. A type is only old from the perspective of an aliasing package. Given we can never enforce this in the ecosystem, I would still like to see standard library aliases implies deprecation strictly (hinted by proper deprecation notices).

I am also suggesting we standardize the notion of deprecation in a parallel discussion and land support for them in our core tools (golint, godoc, etc). Lacking deprecation notices is the biggest problem in the Go ecosystem and is more wide-spread than the problem of gradual code repair.

Merovius commented Dec 6, 2016

@rakyll I'm sympathetic to the use case of having computer-readable deprecation notices; I just object to the notion of a) aliases being that and b) emitting them as compiler warnings.

For a), apart from the fact that I'd like to use aliases productively for other things than moves, it would also only apply for a very small set of deprecations. For example, say I'd want to remove some parameters from a function in a couple of releases; I can't use aliases, really, because the signature of the new API will differ, but I'd still want to announce that. For b), IMHO compiler warnings are universally bad. I think this is mostly in line with what go is already doing, so I don't think it requires justification.

I agree with all of what you are saying about deprecation notices. There already is a syntax for this, apparently: #10909, so the next step to make it more useful would be to enhance tool-support by highlighting them in godoc and have a check that warns about their usage (say go vet, golint or a separate tool alltogether).

@rakyll I agree that the stdlib should start with a conservative use of type aliases, should they be introduced.


Sidebar:

Background for those unaware of the status of deprecation comments in Go and related tooling, as it is rather spread out:

As @Merovius mentions above, there is a standard convention for marking items as deprecated, #10909, see https://blog.golang.org/godoc-documenting-go-code

TL;DR: make a paragraph in the docs of the deprecated item that begins with "Deprecated: " and explains what the replacement is.

There's an accepted proposal for godoc to display deprecated items in a more useful manner: #17056.

@rakyll proposed that golint warn when deprecated items are used: golang/lint#238.


Even if the stdlib takes a conservative stance on use of aliases within the stdlib, I don't think that the existence of a type alias should imply (in any way that is detected mechanically or denoted visually) that the old type is deprecated, even if it always means that in practice.

Doing so would mean one of:

  • scanning other stdlib pakages to see if any type, not explicitly marked as deprecated, is aliased elsewhere
  • hardcoding all stdlib aliases into automated tools
  • only reporting that the old type is deprecated when you are already looking at its replacement, which does not help discovery

When a type alias is introduced because the old type has been deprecated, it needs to be handled by marking the old type deprecated, with a reference to the new type, regardless.

This enables better tooling to exist by allowing it to be simpler and more general: it doesn't need to special case anything or even know about type aliases: it just needs to match "Deprecated: " in doc comments.

An official, if perhaps temporary, policy that an alias in the stdlib is only for deprecation is good, but it should only be enforced with the standard deprecation comments and by disallowing other uses to make it past code review.

Merovius commented Dec 6, 2016

@niemeyer My previous reply got lost due to loss of power :( out of order:

But I'm repeating myself..

FWIW, I found your last reply pretty helpful. It convinced me, that we are more in agreement, than it previously seemed (and than it may still seem to you). There still seems to be miscommunication somewhere, though.

My proposal, though, is about gradual refactoring of code

This is non-contentious, I think. :) I agreed, from the beginning, that your proposal is an interesting alternative to be considered to address the problem. What confuses me are statements like this:

If you delete a method or a type, Go code using it breaks, so these changes need to be done atomically.

I still wonder what your reasoning here is. I understand the unit of atomicity to be a single commit. With that assumption, I simply do not understand why you are convinced that the deletion of a method or type can not first happen in separate, arbitrarily numerous commits in the depending repositories and then, once there is no apparent user anymore (and an ample deprecation interval has passed) the method or type is deleted in a commit upstream (without breaking anything, as no one depends anymore). I agree that there is a certain fuzziness factor around reverse dependencies that don't adhere to the deprecation or that you can not find (or reasonably fix), but that, to me, seems largely independent of the matter at hand; you'll have that problem whenever you apply a breaking change and no matter how you try to orchestrate it.

And, to be fair: The confusion isn't really helped by sentences like

Unless, of course, you live in a walled garden and can reach out to every call site to fix it.

If anything I said gave you the impression that this is the point I'm arguing from, I'd hope you can take a step back and maybe re-read it under the assumption that I am arguing completely from the position of the open source community (if you don't believe me, feel free to look up my previous contributions to this topic; I'm always the first to point out that this by fare more a community problem, than a monorepo problem. Monorepos have ways around this, as you pointed out).

Anyway. I find this just as draining as you. I hope I'll understand your position at some point, though.

Contributor

tombergan commented Dec 6, 2016

simultaneously talk about if and how to support things like the protobuf public imports ...
I think at some point it needs to be debated whether we actually consider wrapper-packages, protobuf public imports or exposing internal package-APIs such a bad thing

nit: I don't think protobuf public imports need to be mentioned as a special secondary use case. They were designed for gradual code repair, as mentioned explicitly in both the internal design doc and even the public documentation, so they already fall under the umbrella of problems described by this issue. Also, I believe type aliases would be sufficient for implementing protobuf public imports. (The proto compiler generates vars, but they are logically const, so "var Enum_name = imported.Enum_name" should be sufficient.)

Contributor

niemeyer commented Dec 7, 2016

@Merovius Thanks for the productive response. Let me try to provide some context:

I still wonder what your reasoning here is. I understand the unit of atomicity to be a single commit. With that assumption, I simply do not understand why you are convinced that the deletion of a method or type can not first happen in separate,

Never said it cannot happen. Let me take a step back and restate more clearly.

We probably all agree that the end goal is two fold: we want working software, and we want to improve the software so we can continue working on it in a sane way. Some of the latter are breaking changes, putting it at odds with the former goal. So there's tension, which means there's some subjectiveness to where the sweet spot lies. The interesting part of our debate lies here.

One helpful way to search for that sweet spot is to think about human interventions. That is, once you do something that requires people to manually modify code to keep it working, inertia takes place. It takes a long time for the relevant portion of all dependent code bases to go through this process. We're asking busy people to do things that in most cases they'd rather not bother.

Another way to look at that sweet spot is likelyhood of working software. It doesn't matter how much we ask people to not use a deprecated method. If it's easily accessible and it solves their problem here and now, most developers will just use it. The common counterargument here is: oh, but then it's their problem when it breaks! But that goes against the the stated goal: we want working software, not being right.

So, hopefully this provides some more insight into why simply moving a type seems unhelpful. For people to actually use that new type at its new home, we need human intervention. When people go over the trouble of manually changing their code, it's best to have an intervention that uses the new type instead of something that will soon change again under their feet in the upcoming future. If we do go over the trouble of adding a language feature to help with refactorings, ideally it would allow people to gradually move their code to that new type, not simply to a new home, for the reasons above.

Merovius commented Dec 7, 2016

Thanks for the explanation. I think I understand your position better now and agree with your assumptions (namely, that people will use deprecated stuff no matter what, so providing any help possible to guide them to the replacement is paramount). FWIW, my naive plan to deal with this problem (no matter which solution for gradual repair we'll go with) is a go-fix like tool to automatically migrate code package-by-package in the deprecation period, but I freely admit hat I haven't yet tried how and if that works in practice.

Contributor

rogpeppe commented Dec 7, 2016

@niemeyer I don't believe your suggestion is workable without a serious disruption to the Go type system.

Consider the dilemma presented by this code:

package old
import "new"
type A adapts new.A
func (a A) NewA() {}

package new
type A struct{}
func (a A) OldA() {}

package main
import (
    "new"
    "old"
    "reflect"
)
func main() {
    oldv := reflect.ValueOf(old.A{})
    newv := reflect.ValueOf(new.A{})
    if oldv.Type() == newv.Type() {
        // The two types are equal, therefore they must
        // have exactly the same method set, so either
        // oldv doesn't have the OldA method or newv doesn't
        // have the NewA method - both of which imply a contradiction
        // in the type system.
    } else {
         // The two types are not equal, which means that the
         // old adapted type is not fully compatible with the old
         // one. Any type that includes either new.A or new.B will
         // be incompatible as one of its components will likewise be
         // unequal, so any code that relies on dynamic type checking
         // will fail when presented with the type that's not using the
         // expected version.
    }
 }

One of the current axioms of the reflect package is that if two types are the same, their reflect.Type values are equal. This is one of the foundations of the efficiency of Go's runtime type conversion. As far as I can see there is no way to implement the "adapts" keyword without breaking this.

Contributor

niemeyer commented Dec 7, 2016

@rogpeppe See the conversation with @rsc about reflection above. The two types are not the same, so reflect would just say the truth and provide details for the adapter when asked about it.

Contributor

rogpeppe commented Dec 7, 2016

@niemeyer If the two types are not the same, then I don't think we can support gradual code repair while moving a type between packages. For example, say we wanted to make a new image package that maintains type compatibility.

We might do:

package newimage
import "image"
type RGBA adapts image.RGB
func (r *RGBA) At(x, y) color.Color {
    return (*image.Buffer)(r).At(x, y)
}
etc for all the methods

Given the aim of gradual code repair, I think it's reasonable to expect that
an image created in the new package is compatible with existing functions
that use the old image type.

Let's assume for the sake of argument that the image/png package has
been converted to use newimage but image/jpeg has not.

I believe that we should expect this code to work:

img, err := png.Decode(r)
if err != nil { ... }
err = jpeg.Encode(w, img, nil)

but, since it does a type assert against *image.RGBA not *newimage.RGBA,
it will fail AFAICS, because the types are different.

Say we made the type assert above succeed, whether the type is *image.RGBA
or not. That would break the current invariant that:

reflect.TypeOf(x) == reflect.TypeOf(x.(anyStaticType))

That is, using a static type assertion wouldn't just assert the static type of a
value but sometimes it would actually change it.

Say we decided that was OK, then presumably we'd also need
to make it possible to convert an adapted type to any interface that any of its compatible
adapted types support, otherwise either new or old code would stop
working when converting to interface types that are compatible with the
type they're using.

This leads to another contradictory situation:

// oldInterface is some interface with methods that
// are only supported by the old type.
type oldInterface interface {
    OldMethod()
}
var x = interface{} = newpackage.Type{}
switch x.(type) {
case oldInterface:
    // This would fail because the newpackage.Type
    // does not implement OldMethod, even though we
    // we just supposedly checked that x implements OldMethod.
    reflect.TypeOf(x).Method("OldMethod")
}

Overall, I think that having two types that are both the same but different
would lead to a very hard to explain type system and unexpected incompatibilities
in code that uses dynamic types.

I support the "type X = Y" proposal. It's simple to explain and does not
disrupt the type system too much.

Member

bcmills commented Dec 7, 2016 edited

@rogpeppe: I believe that @niemeyer's suggestion is to implicitly convert an adapted type to its base type, similar to @josharian's earlier suggestions.

To make that work for gradual refactoring, it would also have to implicitly convert functions with arguments of adapted types; in essence, it would require adding covariance to the language. That's certainly not an impossible task — plenty of languages do allow covariance, particularly for types with the same underlying structure — but it does add a lot of complexity to the type system, particularly for interface types.

That does lead to some interesting edge-cases, as you've noted, but they're not necessarily "contradictory" per se:

type oldInterface interface {
    OldMethod()
}
var x = interface{} = newpackage.Type{}
switch y := x.(type) {
case oldInterface:
    reflect.TypeOf(y).Method("OldMethod")  // ok
    reflect.TypeOf(x).Method("NewMethod")  // ok

    // This would fail because y has been implicitly converted to oldInterface.
    reflect.TypeOf(y).Method("NewMethod")

    // This would fail because accessing OldMethod on newpackage.Type requires
    // a conversion to oldInterface.
    reflect.TypeOf(x).Method("OldMethod")
}
Contributor

rogpeppe commented Dec 7, 2016 edited

// This would fail because accessing OldMethod on newpackage.Type requires
// a conversion to oldInterface.

This still seems contradictory to me. The current model is a very simple one: an interface value has a well defined underlying static type. In the above code we infer something about that underlying type, but when we peek at the value, it doesn't look like what we've inferred. This is a serious (and hard to explain) change to the language in my view.

Contributor

rsc commented Dec 9, 2016

The discussion here seems to be winding down. Based on a suggestion by @egonelbre in #16339 (comment), I've updated the original issue comment (at the top) to include a linked summary of the discussion so far. I will post a new comment, like this one, each time I've updated the summary.

Overall, it seems that the sentiment here is for type aliases rather than generalized aliases. Possibly Gustavo's adapter idea will displace type aliases, but possibly not. It seems a bit complex at the moment, although maybe by the end of the discussion a simpler form will be reached. I suggest that discussion continue for a little while longer.

Merovius commented Dec 9, 2016

I am still not convinced that mutable global vars are "usually a bug" (and in the cases where they are a bug, the race detector is the tool of choice to find that kind of bug). I'd request that, if that argument is used to justify the lack of an extensible syntax, a vet-check is implemented that - say - checks for assignments to global variables in code not exclusively reachable by init() or their declarations. I'd naively think that this isn't particularly hard to implement and it shouldn't be much work to run it over - say - all godoc.org registered packages to see what the use cases for mutable global vars are and whether we do consider all of them bugs.

(I'd also like to believe that, if go grows immutable global vars, they should be part of const-declarations, because that's what they conceptually are and because that would be backwards-compatible, but I acknowledge that this will likely lead to complications around what kind of expressions can be used in array-types, for example and would need more thinking)

Re "Restriction? Aliases of standard library types can only be declared in standard library." -- notably, that would prevent the drop-in usecase for x/image/draw, an existing package which has expressed interest in using aliases. I could also very well imagine, for example, router packages or the like using aliases into net/http in a similar fashion (waves hands).

I also agree with the counter-arguments Re all the restrictions, i.e. I'm in favor of not having any of those.

Contributor

rsc commented Dec 9, 2016

@Merovius, what about mutable exported global vars? It's true that an unexported global might be fine since all the code in the package knows how to handle it properly. It's less obvious that exported mutable globals ever make sense. We made this mistake ourselves a number of times in the standard library. For example there's no completely safe way to update runtime.MemProfileRate. The best you can do is set it early in your program and hope that no package you imported kicked off an initialization goroutine that might be allocating memory. You might well be right about var vs const, but we can leave that for another day.

Good point about x/image/draw. Will add to summary at next update.

I would very much like to assemble a representative corpus of Go code that we could analyze to answer questions like the ones you raise. I started trying to do this a few weeks ago and I ran into some problems. It's a bit more work than it seems like it should be, but it's very important to have that data set, and I expect we'll get there.

@rsc your GothamGo presentation about this topic has been posted on youtube https://www.youtube.com/watch?v=h6Cw9iCDVcU and would make a good addition to the first post.

In the "What other issues does a proposal for type aliases need to address?" section it would be helpful to specify that the answer to "Can methods be defined on types named by alias?" is a hard no. I realize that goes against the decreed spirit of the section, but I've noticed that, in a lot conversations about aliases, here and elsewhere, there are people that immediately reject the concept because they believe that aliases would necessarily allow this and thus cause problems than it solves. It is implicit in the definition, but explicitly mentioning it would short circuit a lot of unnecessary back and forth. Though maybe that belongs in an alias FAQ in the new proposal for aliases, should that be the outcome of this thread.

jimmyfrasche commented Dec 9, 2016 edited

@Merovius any exported package-global mutable variable can be simulated by package-level getter and setter funcs.

Given version n of a package p,

package p
var Global = 0

at version n+1 getters and setters can be introduced and the variable deprecated

package p
//Deprecated: use GetGlobal and SetGlobal.
var Global = 0
func GetGlobal() int {
    return Global
}
func SetGlobal(n int) {
   Global = n
}

and version n + 2 could unexport Global

package p
var global = 0
func GetGlobal() int {
    return global
}
func SetGlobal(n int) {
   global = n
}

(Exercise left to the reader: you could also wrap access to global in a mutex in n + 2 and deprecate GetGlobal() in favor of the more idiomatic Global().)

That's not a fast fix, but it does reduce the problem so that only func aliases (or their present workaround) are strictly necessary for gradual code repair.

jba commented Dec 9, 2016

@rsc One trivial use for aliases you left out of your summary: abbreviating long names. (Probably the only motivation for Pascal, which initially didn't have programming-in-the-large features like packages.) Although it's trivial, it is the only use case where unexported aliases make sense, so maybe worth mentioning for that reason.

Merovius commented Dec 9, 2016

@jimmyfrasche You are correct. I do not like the idea of using getters and setters (just as I don't like to have them for struct fields) but your analysis is, of course, correct.

There is a point to be made about non-repair uses of aliases (e.g. making drop-in replacement packages), but I concede that it weakens the case for var-aliases.

@Merovius agreed on all points. I'm not happy about it either but gotta follow the logic v☹v

iand commented Dec 10, 2016

@niemeyer can you clarify how adapters would assist migrating types where both old and new have a method with the same name but differing signatures. Adding an argument to a method or changing the type of an argument seem like they would be common evolutions of a codebase.

Contributor

niemeyer commented Dec 12, 2016

@rogpeppe Note that this is exactly how it happens today:

type two one

This makes one and two independent types, and whether reflecting or under an interface{}, that's what you see. You can also convert between one and two. The adapter proposal above just makes that last step automatic for adapters. You may not like the proposal for multiple reasons, but there's nothing contradictory about that.

Contributor

niemeyer commented Dec 12, 2016

@iand As in the case of type two one, the two types have completely independent method sets, so there's nothing special about matching names. Before old code bases are migrated they would remain using the old signature under the previous type (now an adapter). New code using the new type would use the new signature. Passing a value of the new type into old code automatically adapts it because the compiler knows the latter is an adapter of the former, and thus uses the respective method set.

Contributor

rsc commented Dec 16, 2016

@niemeyer It seems like there is a lot of complexity hiding behind these adapters that is not fully specified. At this point I think the simplicity of type aliases weighs strongly in their favor. I sat down to list all the things that will need updating just for type aliases, and it's a very long list. The list would certainly be longer for adapters, and I still don't fully understand all the details. I'd like to suggest we do type aliases for now and leave a decision about the relatively heavier adapters to a later time, if you want to work out a full proposal (but again I am skeptical that there aren't dragons lurking there).

Contributor

ulikunitz commented Jan 10, 2017

@rsc Many thanks for the clarifications.

Let's assume:

package a

import "b"

type T1 = b.T2

As far as I understand T1 is essential identical with b.T2 and is therefore a non-local type and no new methods can be defined. The identifier T1 is however re-exported in package a. Is this a correct interpretation?

Contributor

griesemer commented Jan 10, 2017 edited

@ulikunitz that is correct

T1 denotes the exact same type as b.T2. It's simply a different name. Whether something is exported or not is based on it's name alone (has nothing to do with the type it denotes).

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] go/types: implement type aliases
Now a TypeName is just that: a name for a type (not just Named and Basic types
as before). If it happens to be an alias, its type won't be a Named or Basic type,
or it won't have the same name. We can determine this externally.

It may be useful to provide a helper predicate to make that test easily accessible,
but we can get to that if there's an actual need.

The field/method lookup code has become more general an simpler, which is a good sign.
The changes in methodset.go are symmetric to the changes in lookup.go.

Known issue: Cycles created via alias types are not properly detected at the moment.

For #18130.

Change-Id: I90a3206be13116f89c221b5ab4d0f577eec6c78a
Reviewed-on: https://go-review.googlesource.com/35091
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
80d8b69
Contributor

rsc commented Jan 10, 2017

To make @griesemer's reply explicit: yes, T1 is exported from package a (because it is T1, not t1).

CL https://golang.org/cl/35099 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] go/types: remove some more vestiges of prior alias im…
…plementation

For #18130.

Change-Id: Ibec8efd158d32746978242910dc71e5ed23e9d91
Reviewed-on: https://go-review.googlesource.com/35092
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
c80748e

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] go/types: improved Object printing
- added internal isAlias predicated and test
- use it for improved Object printing
- when printing a basic type object, don't repeat type name
  (i.e., print "type int" rather than "type int int")
- added another test to testdata/decls4.src

For #18130.

Change-Id: Ice9517c0065a2cc465c6d12f87cd27c01ef801e6
Reviewed-on: https://go-review.googlesource.com/35093
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
aa1f068

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] go/types: export TypeName.IsAlias so clients can use it
For #18130.

Change-Id: I634eaaeaa11e92fc31219d70419fdb4a7aa6e0b4
Reviewed-on: https://go-review.googlesource.com/35099
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
5ceec42

CL https://golang.org/cl/35100 mentions this issue.

CL https://golang.org/cl/35101 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] cmd/compile, go/importer: define export format and im…
…plement importing of type aliases

This defines the (tentative) export/import format for type aliases.

The compiler doesn't support type aliases yet, so while the code is present
it is guarded with a flag.

The export format for embedded (anonymous) fields now has three modes (mode 3 is new):
1) The original type name and the anonymous field name are the same, and the name is exported:
   we don't need the field name and write "" instead
2) The original type name and the anonymous field name are the same, and the name is not exported:
   we don't need the field name and write "?" instead, indicating that there is package info
3) The original type name and the anonymous field name are different:
   we do need the field name and write "@" followed by the field name (and possible package info)

For #18130.

Change-Id: I790dad826757233fa71396a210f966c6256b75d3
Reviewed-on: https://go-review.googlesource.com/35100
Reviewed-by: Alan Donovan <adonovan@google.com>
49de5f0

CL https://golang.org/cl/35102 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] cmd/compile, go/types, go/importer: various alias rel…
…ated fixes

cmd/compile:
- remove crud from prior alias implementation
- better comments in places

go/types:
- fix TypeName.IsAlias predicate
- more tests

go/importer (go/internal/gcimporter15):
- handle "@" format for anonymous fields using aliases
  (currently tested indirectly via x/tools/gcimporter15 tests)

For #18130.

Change-Id: I23a6d4e3a4c2a5c1ae589513da73fde7cad5f386
Reviewed-on: https://go-review.googlesource.com/35101
Reviewed-by: Alan Donovan <adonovan@google.com>
f011e0c

@gopherbot gopherbot pushed a commit that referenced this issue Jan 10, 2017

@griesemer griesemer [dev.typealias] cmd/compile: various minor cleanups
Also: Don't allow type pragmas with type alias declarations.

For #18130.

Change-Id: Ie54ea5fefcd677ad87ced03466bbfd783771e974
Reviewed-on: https://go-review.googlesource.com/35102
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
ac8421f

CL https://golang.org/cl/35104 mentions this issue.

CL https://golang.org/cl/35106 mentions this issue.

CL https://golang.org/cl/35108 mentions this issue.

@gopherbot gopherbot pushed a commit to golang/tools that referenced this issue Jan 11, 2017

@griesemer griesemer go/gcimporter15: update import/export to handle type aliases
- bimport.go is a 1:1 copy of the respective version in the std
  library: $GOROOT/src/go/internal/gcimporter/bimport.go .

- bexport.go is mimicking the respective code in the cmd/compile.

- isAlias18/19.go are needed because types.TypeName.IsAlias does
  not exist before Go 1.9.

Tested against Go 1.6, 1.7, 1.8, 1.9 (dev.typealias branch).

For golang/go#18130.

Change-Id: Ic46c5850923fab2a35d4dc33850f2b0667c30398
Reviewed-on: https://go-review.googlesource.com/35104
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
de55728

@gopherbot gopherbot pushed a commit to golang/tools that referenced this issue Jan 11, 2017

@griesemer griesemer cmd/godex: handle printing of type aliases
Also: remove go1.5 build tags (x/tools requires Go 1.6 or higher).

For golang/go#18130.

Change-Id: I3d9deee9e87d8794b2884281c0bb53caa5ed6221
Reviewed-on: https://go-review.googlesource.com/35106
Reviewed-by: Alan Donovan <adonovan@google.com>
0d047c8

CL https://golang.org/cl/35120 mentions this issue.

CL https://golang.org/cl/35121 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 12, 2017

@griesemer griesemer [dev.typealias] cmd/compile: type-check type alias declarations
Known issues:
- needs many more tests
- duplicate method declarations via type alias names are not detected
- type alias cycle error messages need to be improved
- need to review setup of byte/rune type aliases

For #18130.

Change-Id: Icc2fefad6214e5e56539a9dcb3fe537bf58029f8
Reviewed-on: https://go-review.googlesource.com/35121
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
b2386df

CL https://golang.org/cl/35129 mentions this issue.

CL https://golang.org/cl/35191 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 12, 2017

@griesemer griesemer [dev.typealias] cmd/compile: improved error message for cyles involvi…
…ng type aliases

Known issue: #18640 (requires a bit more work, I believe).

For #18130.

Change-Id: I53dc26012070e0c79f63b7c76266732190a83d47
Reviewed-on: https://go-review.googlesource.com/35129
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
5c160b2

@gopherbot gopherbot pushed a commit that referenced this issue Jan 12, 2017

@griesemer griesemer [dev.typealias] cmd/compile: a few better comments related to alias t…
…ypes

For #18130.

Change-Id: I50bded3af0db673fc92b20c41a86b9cae614acd9
Reviewed-on: https://go-review.googlesource.com/35191
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
cc2dcce

CL https://golang.org/cl/35233 mentions this issue.

@gopherbot gopherbot pushed a commit to golang/gofrontend that referenced this issue Jan 14, 2017

@ianlancetaylor ianlancetaylor compiler: implement type aliases
This is a start of implementing type aliases (`type T1 = T2`) in the
Go frontend.  This is incomplete, in that the reflection information
is not updated for an embedded type alias.  It is also not well
tested.  Finally, the change to the language has not been approved.
This should be regarded as preliminary work for experimental use.

Update golang/go#18130.

Change-Id: I5de44e4aa56088e0b1ee9d9b5ad2fbc5be40aa07
Reviewed-on: https://go-review.googlesource.com/35120
Reviewed-by: Ian Lance Taylor <iant@golang.org>
ea73a80

@hubot hubot pushed a commit to gcc-mirror/gcc that referenced this issue Jan 14, 2017

ian compiler: implement type aliases
    
    This is a start of implementing type aliases (`type T1 = T2`) in the
    Go frontend.  This is incomplete, in that the reflection information
    is not updated for an embedded type alias.  It is also not well
    tested.  Finally, the change to the language has not been approved.
    This should be regarded as preliminary work for experimental use.
    
    Update golang/go#18130.
    
    Reviewed-on: https://go-review.googlesource.com/35120


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@244460 138bc75d-0d04-0410-961f-82ee72b054a4
9c164db

CL https://golang.org/cl/35268 mentions this issue.

CL https://golang.org/cl/35269 mentions this issue.

bcmills referenced this issue in golang/protobuf Jan 18, 2017

Open

Remove XXX_ fields from generated types. #276

@gopherbot gopherbot pushed a commit to golang/tools that referenced this issue Jan 20, 2017

@griesemer griesemer go/internal/gccgoimporter: support for type aliases
This is essentially a copy of the changes in https://golang.org/cl/35268/
but with Go1.9 specific tests factored out into a separate file with build
tag.

For golang/go#18130.

Change-Id: I235693e1bc6145bb53bfd3b343647704c8e82e61
Reviewed-on: https://go-review.googlesource.com/35269
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
fcfba28

@gopherbot gopherbot pushed a commit that referenced this issue Jan 20, 2017

@griesemer griesemer [dev.typealias] cmd/compile: export/import test cases for type aliases
Plus a few minor changes.

For #18130.

Change-Id: Ica6503fe9c888cc05c15b46178423f620c087491
Reviewed-on: https://go-review.googlesource.com/35233
Reviewed-by: Alan Donovan <adonovan@google.com>
5802cfd

@gopherbot gopherbot pushed a commit that referenced this issue Jan 20, 2017

@griesemer griesemer [dev.typealias] go/internal/gccgoimporter: support for type aliases
For #18130.

Change-Id: Iac182a6c5bc62633eb02191d9da6166d3b254c4c
Reviewed-on: https://go-review.googlesource.com/35268
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
a7c884e

CL https://golang.org/cl/35670 mentions this issue.

CL https://golang.org/cl/35671 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 24, 2017

@ianlancetaylor ianlancetaylor [dev.typealias] test: match gccgo error messages on alias2.go
For #18130.

Change-Id: I9561ee2b8a9f7b11f0851f281a899f78b9e9703e
Reviewed-on: https://go-review.googlesource.com/35640
Reviewed-by: Robert Griesemer <gri@golang.org>
9259f30

CL https://golang.org/cl/35575 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 25, 2017

@mdempsky mdempsky [dev.typealias] cmd/compile: declare methods after resolving receiver…
… type

For #18130.
Fixes #18655.

Change-Id: I58e2f076b9d8273f128cc033bba9edcd06c81567
Reviewed-on: https://go-review.googlesource.com/35575
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
de2e545

CL https://golang.org/cl/35732 mentions this issue.

CL https://golang.org/cl/35733 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 25, 2017

@rsc rsc [dev.typealias] cmd/doc: update for type alias
For #18130.

Change-Id: I06b05a2b45a2aa6764053fc51e05883063572dad
Reviewed-on: https://go-review.googlesource.com/35670
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
9657e0b

@gopherbot gopherbot pushed a commit that referenced this issue Jan 25, 2017

@rsc rsc [dev.typealias] cmd/compile, reflect: fix struct field names for embe…
…dded byte, rune

Will also fix type aliases.

Fixes #17766.
For #18130.

Change-Id: I9e1584d47128782152e06abd0a30ef423d5c30d2
Reviewed-on: https://go-review.googlesource.com/35732
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
9bbb07d

@gopherbot gopherbot pushed a commit that referenced this issue Jan 25, 2017

@rsc rsc [dev.typealias] reflect: add test for type aliases
For #18130.

Change-Id: Idd77cb391178c185227cfd779c70fec16351f825
Reviewed-on: https://go-review.googlesource.com/35733
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
49b7af8

CL https://golang.org/cl/35831 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 26, 2017

@mdempsky mdempsky [dev.typealias] cmd/compile: avoid false positive cycles from type al…
…iases

For #18130.
Fixes #18640.

Change-Id: I26cf1d1b78cca6ef207cc4333f30a9011ef347c9
Reviewed-on: https://go-review.googlesource.com/35831
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
9ecc3ee

@gopherbot gopherbot pushed a commit to golang/proposal that referenced this issue Jan 26, 2017

@rsc rsc design: fixes for 18130-type-alias.md
For golang/go#18130.

Change-Id: Id87b52d537b75ab376d0ec47d8beb524df3b1611
Reviewed-on: https://go-review.googlesource.com/35870
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
bae37ef

@gopherbot gopherbot pushed a commit that referenced this issue Jan 31, 2017

@griesemer griesemer [dev.typealias] spec: use term 'embedded field' rather than 'anonymou…
…s field'

First steps towards defining type aliases in the spec.
This is a nomenclature clarification, not a language change.

The spec used all three terms 'embedded type', 'anonymous field',
and 'embedded field'. Users where using the terms inconsistently.

The notion of an 'anonymous' field was always misleading since they
always had a de-facto name. With type aliases that name becomes even
more important because we may have different names for the same type.

Use the term 'embedded field' consistently and remove competing
terminology.

For #18130.

Change-Id: I2083bbc85788cab0b2e2cb1ff58b2f979491f001
Reviewed-on: https://go-review.googlesource.com/35108
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
f8b4123

CL https://golang.org/cl/36014 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Jan 31, 2017

@rsc rsc all: merge dev.typealias into master
For #18130.

f8b4123 [dev.typealias] spec: use term 'embedded field' rather than 'anonymous field'
9ecc3ee [dev.typealias] cmd/compile: avoid false positive cycles from type aliases
49b7af8 [dev.typealias] reflect: add test for type aliases
9bbb07d [dev.typealias] cmd/compile, reflect: fix struct field names for embedded byte, rune
43c7094 [dev.typealias] reflect: fix StructOf use of StructField to match StructField docs
9657e0b [dev.typealias] cmd/doc: update for type alias
de2e545 [dev.typealias] cmd/compile: declare methods after resolving receiver type
9259f30 [dev.typealias] test: match gccgo error messages on alias2.go
5d92916 [dev.typealias] cmd/compile: change Func.Shortname to *Sym
a7c884e [dev.typealias] go/internal/gccgoimporter: support for type aliases
5802cfd [dev.typealias] cmd/compile: export/import test cases for type aliases
d7cabd4 [dev.typealias] go/types: clarified doc string
cc2dcce [dev.typealias] cmd/compile: a few better comments related to alias types
5c160b2 [dev.typealias] cmd/compile: improved error message for cyles involving type aliases
b2386df [dev.typealias] cmd/compile: type-check type alias declarations
ac8421f [dev.typealias] cmd/compile: various minor cleanups
f011e0c [dev.typealias] cmd/compile, go/types, go/importer: various alias related fixes
49de5f0 [dev.typealias] cmd/compile, go/importer: define export format and implement importing of type aliases
5ceec42 [dev.typealias] go/types: export TypeName.IsAlias so clients can use it
aa1f068 [dev.typealias] go/types: improved Object printing
c80748e [dev.typealias] go/types: remove some more vestiges of prior alias implementation
80d8b69 [dev.typealias] go/types: implement type aliases
a917097 [dev.typealias] go/build: add go1.9 build tag
3e11940 [dev.typealias] cmd/compile: recognize type aliases but complain for now (not yet supported)
e0a05c2 [dev.typealias] cmd/gofmt: added test cases for alias type declarations
2e5116b [dev.typealias] go/ast, go/parser, go/printer, go/types: initial type alias support

Change-Id: Ia65f2e011fd7195f18e1dce67d4d49b80a261203
c47df7a
Contributor

rsc commented Jan 31, 2017

This is now in master, in advance of Go 1.9 opening. Please feel free to sync on master and try things out. Thanks.

Contributor

davecheney commented Feb 2, 2017

Redirected from #18893

package main

import (
        "fmt"
        "q"
)

func main() {
        var a q.A
        var b q.B // i'm a named unnamed type !!!

        fmt.Printf("%T\t%T\n", a, b)
}

What did you expect to see?

deadwood(~/src) % go run main.go
q.A     q.B

What did you see instead?

deadwood(~/src) % go run main.go
q.A     []int

Discussion

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. Instead I have to say

oh, unless it's an alias, in which case you have to remember that it could be an unnamed type, even when you import to from another package.

And worse, it will enable people to promulgate readability anti patterns like

type Any = interface{}

Please do not allow unnamed types to be aliased.

Member

bcmills commented Feb 2, 2017

@davecheney

There is no "code repair" story in moving from one unnamed type to another.

Not true. What if you want to change the type of a method parameter from a named to an unnamed type or vice-versa? Step 1 is to add the alias; step 2 is to update the types which implement that method to use the new type; step 3 is to remove the alias.

(It's true that you can do that today by renaming the method twice. The double-rename is tedious at best.)

And worse, it will enable people to promulgate readability anti patterns like
type Any = interface{}

People can already write type Any interface{} today. What additional harm do aliases introduce in this case?

Contributor

davecheney commented Feb 2, 2017 edited

People can already write type Any interface{} today. What additional harm do aliases introduce in this case?

I called it an anti pattern because that is precisely what it is. type Any interface{}, because it the person writing the code type something a little shorter, that makes a little more sense to them.

On the flip side, all of the readers, who are experienced in reading Go code and recognise interface{} as instinctively as their face in a mirror, have to learn, and relearn, every variant of Any, Object, T, and map them to things like type Any interface{}, type Any map[interface{}]interface{}, type Any struct{} on per package basis.

Surely you agree that package specific names for commonplace Go idioms are a net negative for readability?

Member

bcmills commented Feb 2, 2017

Surely you agree that package specific names for commonplace Go idioms are a net negative for readability?

I do agree, but since the example in question (by far the most common occurrence of that antipatterrn that I've encountered) can be done without aliases, I don't understand how that example relates to the proposal for type aliases.

The fact that the anti-pattern is possible without type aliases means that we must already educate Go programmers to avoid it, regardless of whether aliases to unnamed types can exist.

Member

bcmills commented Feb 2, 2017

And, in fact, type aliases allow for the gradual removal of that antipattern from codebases in which it already exists.

Consider:

package antipattern

type Any interface{}  // not an alias

type Widget interface{
  Frozzle(Any) error
}

func Bozzle(w Widget) error {
  …
}

Today, users of antipattern.Bozzle would be stuck using antipattern.Any in their Widget implementations, and there is no way to remove antipattern.Any with gradual repairs. But with type aliases, the owner of the antipattern package could redefine it like so:

// Any is deprecated; please use interface{} directly.
type Any = interface{}

And now the callers can migrate from Any to interface{} gradually, allowing the antipattern maintainer to eventually remove it.

Contributor

davecheney commented Feb 2, 2017

Contributor

griesemer commented Feb 2, 2017 edited

@davecheney I don't think we have any evidence yet that being able to give an arbitrary type literal a name is harmful. This is also not an unexpected "surprise" feature - it has been discussed in detail in the design document. At this point it makes sense to use this for some time and see where it leads us.

As a counter-example, there are public APIs that use type literals only because the API doesn't want to restrict a client to a specific type (see https://golang.org/pkg/go/types/#Info for instance). Having that explicit type literal may be useful documentation. But at the same time it can be quite annoying to have to repeat that same type literal all over the place; and in fact be an impediment to readability. Being able to conveniently talk about an IntSet rather than a map[int]struct{} w/o being locked in to that one and only IntSet definition is a plus in my mind. That's where type IntSet = map[int]struct{} is exactly right.

Finally, I like to refer back to #18130 (comment) in case you missed it. Unrestricted type declarations using = are really the "elementary" type declaration, and I'm happy that we finally have them in Go.

Contributor

nathany commented Feb 2, 2017

Perhaps type intSet = map[int]struct{} (not exported) would be a better way to use unnamed type aliases, but this sounds like the domain of CodeReviewComments and recommended programming practices, rather than limiting the feature.

Contributor

nathany commented Feb 2, 2017

That said, %T is a handy tool to see types when debugging or exploring the type system. I wonder if there should be a similar format verb that includes the alias? q.B = []int in @davecheney's example.

Member

bcmills commented Feb 2, 2017

@nathany How you you implement that verb? The alias information is not present at runtime. (As far as the reflect package is concerned, the alias is the same type as the thing it is aliased to.)

Contributor

nathany commented Feb 2, 2017 edited

@bcmills I thought that might be the case... 😞

I imagine static analysis tools and editor plugins are still in the picture to help work with aliases, so that's okay.

Member

minux commented Feb 3, 2017

Contributor

rsc commented Feb 3, 2017

@davecheney I added your suggestion to the "Restrictions" section of the discussion summary at the top. Like all restrictions, our general position is that restrictions add complexity (see notes above) and we would likely need to see actual evidence of widespread harm in order to introduce a restriction. Having to change the way you teach Go is not sufficient: any change we make to the language will require changing the way you teach Go.

As noted in the design doc and on the mailing list, we are working on better terminology to make explanations easier.

Contributor

rsc commented Feb 3, 2017

@minux, like @bcmills pointed out, alias information does not exist at runtime (completely fundamental to the design). There is no way to implement a "%T that includes the alias".

Member

minux commented Feb 3, 2017

Contributor

rsc commented Feb 3, 2017

@minux, oh I see, you are replying via email and Github makes the quoted text look like text you wrote yourself. I was replying to the text you quoted from Nathan Youngman, thinking it was yours. Sorry for the confusion.

Contributor

nathany commented Feb 3, 2017 edited

Regarding terminology and teaching, I found the branded types background @griesemer posted quite informative. Thanks for that.

When explaining types and type conversions, baby gophers initially think I'm talking about a type alias, likely due to familiarity with other languages.

Whatever the final terminology, I could imagine introducing type aliases before named (branded) types, especially since declaring new named types is likely to come after introducing byte and rune in any book or curriculum. However, I do want to be mindful of @davecheney's concern to not encourage anti-patterns.

For type intSet map[int]struct{} we say map[int]struct{} is the underlying type. What do we call either side of type intSet = map[int]struct{}? Alias and aliased type?

As for %T, I already need to explain that a byte and rune result in a uint8 and int32, so this is no different.

If anything, I think type aliases will make byte and rune easier to explain. IMO, the challenge will be knowing when to use named types vs. type aliases, and then being able to communicate that.

Contributor

griesemer commented Feb 3, 2017 edited

@nathany I think it makes a lot of sense to introduce "alias types" first - though I wouldn't use the term necessarily. The newly introduced "alias" declarations are simply regular declarations that don't do anything special. The identifier on the left and the type on the right are one and the same, they denote identical types. I'm not even sure we need the terms alias or aliased type (we don't call a constant name an alias, and the constant value the aliased constant).

The traditional (non-alias) type declaration does more work: It first creates a new type from the type on the right before binding the identifier on the left to it. Thus the identifier and the type on the right are not the same (they only share the same underlying type). This is clearly the more complicated concept.

We do need a new term for these newly created types because any type can now have a name. And we need to be able to refer to them since there are spec rules referring to them (type identity, assignability, receiver base types).

Here's another way to describe this, which may be useful in a teaching environment: A type may either be colored or uncolored. All predeclared types, and all type literals are uncolored. The only way to create a new colored type is via a traditional (non-alias) type declaration which first paints (a copy of) the type on the right with a brand-new, never-before used color (stripping the old color, if any, entirely in the process) before binding the identifier on the left to it. Again, the identifier and the (implicitly and invisibly created) colored type are identical, but they are different from the (differently colored or uncolored) type written down on the right.

Using this analogy, we can reformulate various other existing rules as well:

  • A colored type is always different from any other type (because each type declaration uses a brand-new, never-before used color).
  • Methods may only be associated with receiver base types that are colored.
  • The underlying type of a type is that type stripped off all its color.
    etc.
Contributor

nathany commented Feb 3, 2017 edited

we don't call a constant name an alias, and the constant value the aliased constant

good point 👍

I'm not sure if the coloured vs. uncoloured analogy is easier to understand, but it does demonstrate that there is more than one way to explain the concepts.

Traditional named/branded/coloured types certainly require more explanation. Especially when a named type can be declared using an existing named type. There are fairly subtle differences to keep in mind.

type intSet map[int]struct{} // a new type with an underlying type map[int]struct{}

type myIntSet intSet // a new type with an underlying type map[int]struct{}

type otherIntSet = intSet // just another name (alias) for intSet, add methods to intSet (only in the same package)

type literalIntSet = map[int]struct{} // just another name for map[int]struct{}, no adding methods

It's not insurmountable though. Assuming this lands in Go 1.9, I suspect we'll be seeing 2nd editions of several Go books. 😉

I regularly refer to Go spec for the accepted terminology, so I'm very curious what terms are chosen in the end.

Member

bcmills commented Feb 3, 2017

We do need a new term for these newly created types because any type can now have a name.

Some ideas:

  • "distinguished" or "distinct" (as in, can be distinguished from other types)
  • "unique" (as in, it is a type different from all other types)
  • "concrete" (as in, it is an entity that exists in the runtime)
  • "identifiable" (as in, the type has an identity)
Contributor

griesemer commented Feb 3, 2017

@bcmills We've been thinking about distinguished, unique, distinct, branded, colored, defined, non-alias, etc. types. "Concrete" is misleading because an interface can be colored as well, and an interface is the incarnation of an abstract type. "Identifiable" also seems misleading because a "struct{int}" is just as identifiable as any explicitly (non-alias) named type.

Member

bcmills commented Feb 3, 2017

I would recommend against:

  • "colored" (in non-programming contexts the phrase "colored types" carries strong racial-bias connotations)
  • "non-alias" (it's confusing, since the target of the alias may or may not be what was formerly called a "named type")
  • "defined" (aliases are defined too, they're just defined to be aliases)

"branded" could work: it carries a "types as cattle" connotation but that doesn't strike me as intrinsically bad.

Unique and distinct seem like the stand out options so far.

They're simple and understandable without a lot of additional context or knowledge. If I didn't know the distinction, I think I'd at least have a general sense of what they imply. I can't say that about the other choices.

Once you learn the term it doesn't matter, but a connotative name avoids unnecessary barriers to internalizing the distinction.

Contributor

rsc commented Feb 3, 2017

This is the definition of a bikeshed argument. Robert has a pending CL at https://go-review.googlesource.com/#/c/36213/ that seems perfectly fine.

CL https://golang.org/cl/36213 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Feb 6, 2017

@griesemer griesemer spec: introduce alias declarations and type definitions
To avoid confusion caused by the term "named type" (which now just
means a type with a name, but formerly meant a type declared with
a non-alias type declaration), a type declaration now comes in two
forms: alias declarations and type definitions. Both declare a type
name, but type definitions also define new types.

Replace the use of "named type" with "defined type" elsewhere in
the spec.

For #18130.

Change-Id: I49f5ddacefce90354eb65ee5fbf10ba737221995
Reviewed-on: https://go-review.googlesource.com/36213
Reviewed-by: Rob Pike <r@golang.org>
56c9b51
Contributor

LionNatsu commented Feb 7, 2017

I want to bring up the issue of go fix again.

To be clear that I am not suggesting 'take down' the alias. Maybe it is some thing useful and suitable for other jobs, that is another story.

It's something very important IMO that the title is about moving type. I have no wish to perplex the issue. Our aim is to deal with a kind of interface changes in a project. When we come to a change on interface, it is not true that we hope all the users use these two interface (old & new) as the same eventually, and that is why we say 'gradual code repair'. We hope that users remove/change the usage of the old one.

I still consider tool as the best method to repair the code, something like the idea which @tux21b suggested. For example:

$ cat "$GOROOT"/RENAME
# This file could be used for `go fix`
[package]
x/net/context=context
[type]
io.ByteBuffer=bytes.Buffer

$ go fix -rename "$GOROOT"/RENAME [packages]
# -- or --
# use a standard libraries rename table as default
$ go fix -rename [packages]
# -- or --
# include this fix as default
$ go fix [packages]

The only reason @rsc say no here is that changes will affect other tools. But I think it's not true in this work flow: if there is an out-of-date package (e.g. a dependency) uses the deprecated name/path of package, e.g. x/net/context, we can fix the code at first, just like the doc says how to migrate code to new version, but not hard-coding, via a configurable table in text format. Then you may use any tools whenever you like as same as Go of the new version. There is a side-effect: it will modify code.

Contributor

rsc commented Feb 7, 2017

@LionNatsu, I think you are right, but I think that's a separate issue: should we adopt conventions for packages to explain to potential clients how to update their code in response to API changes in a mechanical way? Perhaps, but we'd have to figure out what those conventions are. Can you open a separate issue for this topic, pointing back at this conversation? Thanks.

CL https://golang.org/cl/36691 mentions this issue.

@gopherbot gopherbot pushed a commit that referenced this issue Feb 10, 2017

@griesemer griesemer spec: refer to "not defined type" rather than "unnamed type" in conve…
…rsions

We missed this in https://golang.org/cl/36213.
Thanks to Chris Hines for pointing it out.

For #18130.

Change-Id: I6279ab19966c4391c4b4458b21fd2527d3f949dd
Reviewed-on: https://go-review.googlesource.com/36691
Reviewed-by: Ian Lance Taylor <iant@golang.org>
866f63e
Member

crawshaw commented Feb 26, 2017

With this proposal at tip, I can now create this package:

package safe

import "unsafe"

type Pointer = unsafe.Pointer

which allows programs to create unsafe.Pointer values without importing unsafe directly:

package main

import "safe"

func main() {
	x := []int{4, 9}
	y := *(*int)(safe.Pointer(uintptr(safe.Pointer(&x[0])) + 8))
	println(y)
}

The original alias declarations design doc calls out this as explicitly supported. It is not explicit in this newer type alias proposal, but it works.

On the alias declaration issue the rational for this is: "The reason we allow aliasing for unsafe.Pointer is that it's already possible to define a type that has unsafe.Pointer as underlying type." #16339 (comment)

While that's true, I think allowing an alias of unsafe.Pointer introduces something new: programs can now create unsafe.Pointer values without explicitly importing unsafe.

To write the program above before this proposal, I would have to move the safe.Pointer cast into a package that imports unsafe. This may make it a bit harder to audit programs for their use of unsafe.

Member

bcmills commented Feb 26, 2017 edited

@crawshaw, couldn't you have just done this before?

package safe

import (
  "reflect"
  "unsafe"
)

func Pointer(p interface {}) unsafe.Pointer {
  switch v := reflect.ValueOf(p); v.Kind() {
  case reflect.Uintptr:
    return unsafe.Pointer(uintptr(v.Uint()))
  default:
    return unsafe.Pointer(v.Pointer())
  }
}

I believe that would allow exactly the same program to compile, with the same lack of import in package main.

(It wouldn't necessarily be a valid program: the uintptr-to-Pointer conversion includes a function call, so it doesn't meet the unsafe package constraint that "both conversions must appear in the same expression, with only the intervening arithmetic between them". However, I suspect it would be possible to construct an equivalent, valid program without importing unsafe from main by making use of things like reflect.SliceHeader.)

Contributor

dr2chase commented Feb 27, 2017

Seems like exporting a hidden unsafe type is just another rule to add to the audit.

Member

crawshaw commented Feb 27, 2017

Yes, I wanted to point out that directly aliasing unsafe.Pointer makes code harder to audit, enough so that I hope no one ends up doing so.

Contributor

griesemer commented Feb 27, 2017

@crawshaw Per my comment, this was also true before we had type aliasing. The following is valid:

package a

import "unsafe"

type P unsafe.Pointer
package main

import "./a"
import "fmt"

var x uint64 = 0xfedcba9876543210
var h = *(*uint32)(a.P(uintptr(a.P(&x)) + 4))

func main() {
	fmt.Printf("%x\n", h)
}

That is, in package main, I can do unsafe arithmetic using a.P even though there's no unsafe package and a.P is not an alias. This was always possible.

Is there something else you are referring to?

Member

crawshaw commented Feb 27, 2017

My mistake. I thought that didn't work. (I was under the impression that the special rules applied to unsafe.Pointer would not propagate to new types defined from it.)

Contributor

griesemer commented Feb 27, 2017

The spec is actually not clear on this. Looking at the implementation of go/types, it turns out that my initial implementation required unsafe.Pointer exactly, not just some type that happened to have an underlying type of unsafe.Pointer. I just found #6326 which is when I changed go/types to be gc compliant.

Perhaps we should disallow this for regular type definitions and also disallow aliases of unsafe.Pointer. I can't see any good reason for allowing it and it does compromise the explicitness of having to import unsafe for unsafe code.

Contributor

griesemer commented Feb 27, 2017

I created #19306.

Owner

bradfitz commented May 3, 2017

This happened. I don't think anything remains here.

bradfitz closed this May 3, 2017

@gopherbot gopherbot pushed a commit that referenced this issue Jun 7, 2017

@bradfitz bradfitz builtin: use type aliases for rune and byte
As motivated by https://golang.org/design/18130-type-alias which says:

https://github.com/golang/proposal/blob/master/design/18130-type-alias.md#relationship-to-byte-and-rune

> The language specification already defines byte as an alias for
> uint8 and similarly rune as an alias for int32, using the word alias
> as an informal term. It is a goal that the new type declaration
> semantics not introduce a different meaning for alias. That is, it
> should be possible to describe the existing meanings of byte and
> uint8 by saying that they behave as if predefined by:
>
>     type byte = uint8
>     type rune = int32

So, do that. Seems to work.

Updates #18130

Change-Id: I0740bab3f8fb23e946f3542fdbe819007a99465a
Reviewed-on: https://go-review.googlesource.com/45017
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
703a9ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment