-
Notifications
You must be signed in to change notification settings - Fork 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
Static type metadata guided shorthands and features #3905
Comments
Finding good names for positional parameters is not always possible - especially for callbacks. E.g. the parameter of When we use the current syntax, we can assign a more semantic name like The same problem of finding a good name can be encountered across dart APIs. The parameter of I find a bit strange that after introducing records, dart hasn't allowed the names like foo(
$1: if (cond) 42,
$2: "hello"
); |
(Cont-d) foo(=>a + b);
// becomes
foo((a, b) { print(a); return a+b; } These expressions have nothing in common, thus breaking the principle of syntax similarity/dissimilarity between similar/dissimilar "things". The current syntax doesn't scale well either, but foo((a, b)=> a+b);
foo((a, b) { return a + b; }); at least have a common mylist.map(:{ print($1); $1*$1 });
rabbits.map((rabbit) { print(rabbit); rabbit.age }; // no semicolon after the last expression, no explicit "return"
// plus evaluation syntax
foo(${ print("in anonymous function"); 2+2 });
// as a replacement for IIFE
foo((){ print("in anonymous function"); return 2+2; }()); Nitpick: I browsed the dart core API trying to discern the naming conventions for positional parameters in current libraries. |
My main worry about making names of positional parameters significant, is that existing code wasn't written for it, and after the feature has launched, changing those names is a breaking change. The If function literal shorthand uses the names of the context type, like a callback parameter, that's a position where people have so far been even less inclined to consider the names than they would in directly callable APIs, after all they're the only one who'll call the function. If they have a name at all. Falling back on $1…$n if the parameters have no name is probably fine. IMO: The arguments to |
I have to disagree on radians, but this is beside the point here (it can be a subject for a philosophical discussion). For normal functions (not callbacks) it's all about conditional parameters, which are rarely needed, and even then, For callbacks, you will have variable names falling from the sky. When we denote the parameters as (As for |
This is more of a meta-issue around the use of static information, some that is not directly part of a type, used to infer more than just types.
There are multiple features that try to rely on a static type, sometimes the context type, to allow shorthands or other features, and more features that probably ould do so.
.value
: Enum "shorter dot" syntax sugar #1183=> expr
: Allow more concise function literal argument #343makePoint(y: 42, x: 37)
wherex
andy
are positional parameters. (Probably proposed somewhere).These all rely on the static type of something, and sometimes on metadata related to that static type.
void Function(int Function(int a, int b) compare)
with a shorthand function=> a - b
could default to using the positional parameter names of the context function type.Those are all potentially useful features, but are not really viable in Dart without formally defining the flow of such metadata through type inference and type operations (UP or similar).
If this is something we want to do, we'll have to do that work on propagating the metadata (parameter names, parameter default scopes here, possibly other things too in the future).
If we do that work for one of these features, it may enable some of the other features.
We've generally shied away from giving semantics to positional parameter names outside of their scope, because doing so makes it a breaking change to rename a parameter, which it currently isn't. We'd have to accept that change if we want to use the names, or allow a function to opt in to its names being public - but if we expect most/all positional parameters to be public in the future, it might be better to just opt out legacy code, and have people do their final renaming before upgrading to the language version which changes it.
So, at a higher level, is this kind of "static metadata" based programming shorthands or aides something we want at all?
And if so, how far are we willing to go?
(One can see
dynamic
andvoid
as similar "metadata" on a type that is reallyObject?
. We're treating them as separate types, which works because the information follows the type, and we've had to define things like MORE_TOP to account for the static difference - that is, we've modified our type operations to preserve and combine this particular kind of metadata. So there is precedence.)The text was updated successfully, but these errors were encountered: