Repository navigation
Proposal: Alias declarations for Go #16339
Description
Abstract
We propose to add alias declarations to the Go language. An alias declaration introduces an alternative name for an object (type, function, etc.) declared elsewhere. Aliases simplify splitting packages because clients can be updated incrementally, which is crucial for large-scale refactoring.
Motivation
During refactoring, it is often desirable to split an existing package into multiple packages. Exported objects such as types, functions, etc. that move from one package to another will require clients to be updated accordingly. In a continuous build environment, build breakages are the consequence if not all clients can be updated simultaneously.
This is a real issue in large-scale systems such as we find at Google and other companies because the number of dependencies can grow into the hundreds if not thousands. Client packages may be under control of different teams and evolve at different speeds. Updating a large number of client packages simultaneously may be close to impossible. This is an effective barrier to system evolution and maintenance.
Go trivially permits constants to refer to other constants, possibly from another package. Similarly, if a function moves from one package to another, a “forwarder” function that simply calls the moved function may be left behind so clients can continue to refer to the old package. These techniques enable incremental update of clients without breaking a continuous build system.
No such work-around exists for types and variables. To address the issue, we propose the notion of a general alias declaration. An alias declaration introduces an alternative name for an object (constant, type, variable, or function) declared elsewhere, possibly in another package. An alias may be exported, so clients can refer to on object via the package that declares the object, or any package exporting an alias to an object, and get the same object.
Alias declarations are designed such that they fit seamlessly in the existing language spec without invalidating backward-compatibility or any other aspect of the Go 1 guarantees. Tools that process Go code which will require changes to support alias declarations.
Alias declarations are a compile-time mechanism and don’t incur any runtime costs.
The design document (forthcoming) describes alias declarations in detail.
Added July 25, 2016: Link to design document: https://github.com/golang/proposal/blob/master/design/16339-alias-decls.md
Activity
griesemer commentedon Jul 12, 2016
Pending design doc: https://go-review.googlesource.com/24867
nicerobot commentedon Jul 12, 2016
Personally, just off the top of my head 👎 , I think I'm not interested in language features specifically targeting refactoring. I vendor libraries (using submodules or subtrees) for precisely this reason. That is, all my vendors can change their implementations at will without breaking my builds and my repos can uptake more current libraries as time permits.
In fact, i'm inclined to think this is a very bad idea because you can end up with aliases to libraries that further change their interfaces and break the aliases. For example, let's just say I ended up with 50 libraries that alias types in my library. How do I know about those dependencies? And if i choose to make a drastic change and privatize or rename the type, let's say i even gave a year notice of its deprecation. Do the aliases get that notification? How? And after the deprecation deadline, all those libraries that weren't changed now break.
gopherbot commentedon Jul 13, 2016
CL https://golang.org/cl/24867 mentions this issue.
Merovius commentedon Jul 13, 2016
What is the significance of forbidding aliasing
unsafefunctions?In particular, I see a conflict in reasoning: For
unsafe.Pointerit is argued, that the same effect is already possible, so forbidding it makes no sense. But it is also possible to create a wrapper for a function inunsafeand thus effectively get the same effect (as outlined elsewhere in the doc), so it shouldn't make sense either.matttproud commentedon Jul 13, 2016
I am confident this would have made a painful LSC refactoring I conducted several months ago more palatable. I appreciate your attention to these shortcomings.
One issue: how much is it worth to use
->given its similarity to the preexisting<-in channel operations? As someone who already knows Go, the potential ambiguity is not a problem. For newcomers, however, the cognitive impact could be profound.(edited for clarity)
cespare commentedon Jul 13, 2016
@matttproud Go does not already have
->, only<-.If I may jump on the syntax bikeshed bandwagon, though, I think that
=>(mentioned in the proposal) would be slightly better than->because it's a little further removed from<-and because it resembles=.josharian commentedon Jul 13, 2016
I'm inclined in favor of a more limited form of the proposal. Here's a first round of comments, in no particular order:
val -> chto try to sendvaloverchan. We'd have to take special care to give very good error messages around this. This is an advantage of=>over->. (Not that I think it is a necessarily a good idea, but if restricted to the top level, then:=could be used as the alias syntax.)go docand other documentation friends could automatically "follow" aliases if the alias itself doesn't have any associated documentation.dlsniper commentedon Jul 13, 2016
I can't speak yet if it's a good thing or bad (I lean towards bad but I need more time to understand the ramifications).
Meanwhile, I would see this as
type from alias toinstead oftype from -> toor eventype alias from to, that way it's at least explicit what that symbol means and it's not confused with channels / future channel ops(?)Kunde21 commentedon Jul 13, 2016
I don't like how closely the
->identifier resembles the<-identifier, especially with such different meanings. Might@be a better option?Rather than reading the alias as "this points to", I see it as
Xis defined atL1.X.Beyond that, I really don't like the scope that this proposal allows. Refactoring is a difficult problem, but it should really be driven by the package owner(s). To that point, I'd add these restrictions:
go vetorgolintshould discourage using aliased definitions. Not only would this make dependent packages aware of the aliasing, it would encourage adoption of the desired refactoring.goimportsis able to automatically update the alias and add the import. Adoption would be as simple asgo get -u ./... && goimports -w ..jimmyfrasche commentedon Jul 13, 2016
I definitely prefer this to import shenanigans.
Something like
seems like it could be confusing. Though obviously one should not do such a thing. Vet check?
What would the effect of
be? That seems like it would have to be explicitly illegal.
As far as the syntax, I do like @ better than ->, very mnemonic. Totally fine with -> though—it'd stop feeling weird after a couple uses.
mem commentedon Jul 13, 2016
I think @Kunde21's point is an important one: since this is presented as a method to facilitate long refactorings, that implies it's addressing a temporary need. Yet at GopherCon one of the immediate questions was "can this be used to expose details of internal packages?" (I'd add "in a controlled manner") and that implies a more permanent situation. I'm not saying for or against, I'm saying that perhaps the motivation for this needs some refinement.
josharian commentedon Jul 13, 2016
I presume that the following is illegal, but at first glance I don't see what in the proposal forbids it:
373 remaining items
atdiar commentedon Nov 7, 2016
@rogpeppe
Off the top of my head, that shouldn't be an issue since this field is the empty string unless exported.
And aliases allow to transitively export and rename API elements as per the current proposal.
I'd like to emphasize, again, that I don't see the context move as the best use case to present for this proposal. The original use case @gri presented seems more befitting. The only thing is that, strictly speaking, an API is refactored; not so much the code/text. That's if we care about compatibility (backward and forward). As seen in the context case, build tags are required which has repercussions on ABI compatibility (for people who do not publish their source code, that could have implications).
trebe commentedon Nov 7, 2016
If type T = otherpkg.S is really to assist package refactoring, then I suggest that we add the following two requirements:
bcmills commentedon Nov 7, 2016
@trebe
That kind of local aliasing isn't just for "lazy typists". If you're dealing with a generated API (e.g. a protobuf package), you can easily end up with a name like
SomeStupidTypeName_SomeOneofField_SomeOtherFieldthat you need to use frequently. Aliasing that locally to something likeotherFieldcan make the code more readable, not just reduce typing.Merovius commentedon Nov 7, 2016
@trebe Refactorings are not the only use case mentioned in this proposal for aliases (which is a strength of the proposal. It means it solves several different problems). Two other use cases mentioned (so far) where a) implementing protocol buffer public imports and b) allowing drop-in augmentations of existing packages (like x/image/draw). There might be others that I'm missing right now.
nathany commentedon Nov 8, 2016
@Merovius Aliases were also proposed as a way to design a new API that is split into sub-packages for organizational purposes, while the clients need not care about those sub-packages.
See #16339 (comment)
(not something I've personally run into yet)
gopherbot commentedon Nov 10, 2016
CL https://golang.org/cl/33019 mentions this issue.
go/types: remove unused alias-related testdata files
rogpeppe commentedon Nov 14, 2016
@atdiar
I was talking about the value returned by the Type.PkgPath, not the value in the StructField. This is always non-empty for named non-built-in types.
atdiar commentedon Nov 14, 2016
@rogpeppe You're right, confusion is mine ( was thinking of Method.PkgPath)
In any case, since this is a compile time mechanism only, I think that the PkgPath should be the one corresponding to the package in which the object definition was written.
gopherbot commentedon Nov 18, 2016
CL https://golang.org/cl/33365 mentions this issue.
spec: remove => (alias) operator from Operators and Delimiters section
rsc commentedon Nov 27, 2016
Locking because aliases are no longer proposed.
rsc commentedon Dec 1, 2016
Although aliases were not adopted for Go 1.8, we still want to solve the underlying problem for Go 1.9. I’ve filed a new issue #18130 to start a discussion about our options. Please see that issue and the corresponding background article “Codebase Refactoring (with help from Go)” for more information. Thanks.