Skip to content

Enhanced Object Literals #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
listepo opened this issue Feb 4, 2019 · 5 comments
Open

Enhanced Object Literals #205

listepo opened this issue Feb 4, 2019 · 5 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@listepo
Copy link

listepo commented Feb 4, 2019

Shorter syntax for common object property definition idiom.
http://es6-features.org/#PropertyShorthand

It would also be nice for named parameters.

@lrhn
Copy link
Member

lrhn commented Mar 1, 2019

The idea seems to be that in a position where a name and value is required, but only a single identifier is provided, treat that identifier as both the name and the value.

Dart doesn't really have any positions where that is the case syntactically because the only place we have names and values is named arguments, and there a single expression can also mean a positional argument. We would have to use the static type of the function being called to distinguish whether foo(x) means foo(x) or foo(x: x). That's too fragile and error-prone.

What we could do instead would be introducing a shorthand syntax like foo(:x) which marks this as a named argument, but leaves the name to be inferred from the expression, which must then be a single identifier.
It would make forwarding named parameters much easier:

class Something extends Other {
   foo(int a, {int bar, int baz, int qux}) => super.foo(a, :bar, :baz, :qux);
}

(It's a good syntax, though, which we then can't use for anything else).

I think I'd prefer a simple way to forward all arguments over this:

class Something extends Other {
   foo(int a, {int bar, int baz, int qux}) = super.foo;
}

but it's not impossible.

@listepo
Copy link
Author

listepo commented Mar 1, 2019

@lrhn thanks for your answer, I understood. I like shorthand syntax - foo(:x)

@lrhn lrhn added the feature Proposed language feature that solves one or more problems label Mar 4, 2019
@rrousselGit
Copy link

I'd suggest putting the : after the variable name – foo(x:)

That's because auto-complete naturally write x:

@mateusfccp
Copy link
Contributor

mateusfccp commented Jun 1, 2021

I am looking forward to this. I like foo(:x) syntax.

There are many cases where one simply forward from one constructor to another, and it becomes tedious to repeat a: a every time. #698 would also be very helpful in these cases.

@mateusfccp
Copy link
Contributor

mateusfccp commented Jun 1, 2021

@lrhn

I think I'd prefer a simple way to forward all arguments over this:

class Something extends Other {
   foo(int a, {int bar, int baz, int qux}) = super.foo;
}

but it's not impossible.

For "all-constructors" cases it would be great. Why couldn't we be still more directly? Like foo = super.foo? I know there may be some disambiguation work here and possibly an alternative syntax, but the ideia is to also omit the constructor parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

4 participants