Open nex3 opened 1 year ago
It's definitely going to be briefer, and is likely to get used a lot.
We'd have to define which expressions counts as having a "name" that can be used as parameter/field name.
: prefix.varName
or : Class.staticVarName
(and : prefix.Class.staticVarName
).: expression.getterName
in general, so any expression ending in .identifier
.: (expression.getterName)
Those are fairly safe. Then there are the more speculative ones:
:foo.v1 ?? bar.v1
- both have the same name, use that. Similar for ||
, &&
and ?
/:
.:foo.v1 ?? (throw "Nope")
- only one branch has a name, use that.:await v1
- the await doesn't have to affect the name, the name's meaning is likely to still apply.: ! foo.hasBar
- probably not useful since !
changes, flips, the meaning, so why should the name still match?: ++varName
, : varName++
can both work, the name and value still match conceptually.: foo = 42
. Probably too weird, even if there is a name right there. :foo = bar
using bar
as the name (or whatever the name of the RHS expression is), and ignore the LHS.The analogous feature in JS only works for plain identifiers, but it would be pretty neat to be able to use at least expressions ending in .identifier
. Most of the others seem confusingly weird to me.
I really like this idea. As @nex3 suggests, I'd probably want to stick to simple identifiers and maybe identifiers wrapped in prefix expressions (which we allow for patterns). Going beyond that gets weird fast.
Terse bool
s would be nice too:
(+:foo, -:bar)
=> (foo: true, bar: false)
The pattern-matching syntax introduces the constructs
:var foo
and:foo
to declare a variable whose name matches a field on the object being matched. The purpose of this is to reduce duplication, to users don't have to explicitly writefoo: var foo
.I propose using this syntax as a way to reduce duplication when passing named arguments (or constructing records). Specifically, make
foo(:argument)
equivalent tofoo(argument: argument)
. Similarly, record literals could be declared using(:field)
instead of(field: field)
. This would help reduce duplication and ease acclimation to Dart for users coming from JS, which supports similar behavior when using maps as named arguments (in JS,foo({argument})
is equivalent tofoo({argument: argument})
).