carbon-lang icon indicating copy to clipboard operation
carbon-lang copied to clipboard

More powerful aliasing feature

Open josh11b opened this issue 8 months ago • 5 comments

We currently have two features for giving another name to an entity, alias and let template...:!. These both have significant limitations:

  • let template...:! is not concise, and requires specifying the type.
  • let template...:! changes the phase of symbolic values to template.
  • Neither can be parameterized.
  • alias can't give a name to a specific, which means there are situations where specific instantiations of generic types can't be substituted for non-generic types
  • Using a name from another name scope without renaming it still involves repeating the name.

I think it would be good to replace alias by a more powerful construct (or constructs) that can address these use cases:

  • Has a form that allows pulling in A.B.C with the name C without writing C twice:
import Foo;
[USING] Foo.Bar.Baz;
  • Giving a name to a member of a specific. For example, to use a method from a class that you have an implicit conversion to:
class ContainerView(T:! type) {
  fn Enumerate[self: Self]()...;
}
class Container(T:! type) {
  [USING] ContainerView(T).Enumerate;
}
  • Setting the value of an associated constant:
impl i32 as AddWith(i8) {
  [WHERE] Result = i32;
}
  • Parameterized type aliases (as supported by C++ since C++11)

  • Evolution use cases like renaming a namespace (currently can only be done with alias).

  • Give another name to something without forcing it to template phase:

class Container(T:! type) {
  [WHERE] ElementType = T;
}
  • This example also shows another use case: giving a name to something that can't be named from outside this scope.

josh11b avatar Feb 26 '25 23:02 josh11b