carbon-lang
carbon-lang copied to clipboard
More powerful aliasing feature
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.
aliascan'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.Cwith the nameCwithout writingCtwice:
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.