language icon indicating copy to clipboard operation
language copied to clipboard

[extension type] Automatic completion type

Open medz opened this issue 2 years ago • 1 comments

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')];

medz avatar Dec 15 '23 14:12 medz

  • #1955

medz avatar Dec 19 '23 14:12 medz

I believe this is a duplicate of #3614.

munificent avatar Mar 11 '24 22:03 munificent