[extension type] Automatic completion type
basic
The extension type will be released soon in version 3.3, and it can currently be constructed via as:
extension type Test(String message) {
void echo() => print(message);
}
void echo(Test message) => message.echo();
void main() => echo("Hello, Dart" as Test);
I have a new idea, why can't we ignore as T and implement automatic construction? For example:
void main() => echo("Hello, Auto factory");
Constructor and factory selection
Beyond that, I think a lot of it is already clear what type of parameter position is required, and we can ignore the type flag and just use . to select static members of the type:
enum E { one, two }
void echo(E select) => print('Selected ${E}.${selecte.name}');
void main() {
echo(.one); // Full is `E.one`
echo(.two);
}
The same is true for the construction entry selection of extension type/class:
extension type E(String message) {
factory E.pre(String message) => '[PRE] $message';
// OR
// factory E.pre(String message) => E('[PRE] $message');
}
void echo(E demo) => print(demo.message);
void main() {
echo('One');
echo(.pre('Two'));
}
Why do this?
First of all, this should be a syntactic sugar (🍬). The reason is that I have a type that has several factory functions, and the type is wrapped into a List.
Without it, the code:
final List<PathConponent> paths = [
PathConponent('a'),
PathCompoinent.param('b'),
// ... and more
];
It's much simpler when you have autocomplete types:
final List<PathConponent> paths = ['a', .params('b')];
- #1955
I believe this is a duplicate of #3614.