-
Notifications
You must be signed in to change notification settings - Fork 207
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
Whether the variable name in the tuple should be forbidden? #3044
Comments
Why should we disallow names for positional fields in record types? Is it confusing that it's allowed? Is it too easy to forget the We deliberately allow names for positional record fields in record types, the same way we allow names for positional parameters in function types, even if they have no functional effect. They can be used for documentation. So, it's fine to write: Iterable<(K key, V value)> mapEntries<K, V>(Map<K, V> map) => map.entries.map((e) => (e.key, e.value)); The names are useful here. We currently disallow names that would conflict with another field's name. So, all in all, the current behavior is consistent and by design. If you don't want to write names for positional record fields, you can obviously choose to not do so. |
Agreed with @lrhn. While names on positional fields in record types are somewhat confusing, it's also useful for documentation and consistent with positional parameters in function types. This was a deliberate design choice. |
We can use the name of the function parameter, but the position parameter name of the tuple can only be replaced with a symbol such as $1. The return value type is (int a, int b), res.a is not allowed, it can only be res.$1
…---Original---
From: "Lasse R.H. ***@***.***>
Date: Fri, May 5, 2023 18:34 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [dart-lang/language] Whether the variable name in the tupleshould be forbidden? (Issue #3044)
Why should we disallow names for positional fields in record types?
Is it confusing that it's allowed? Is it too easy to forget the {...} around the named fields?
We deliberately allow names for positional record fields in record types, the same way we allow names for positional parameters in function types, even if they have no functional effect. They can be used for documentation.
So, it's fine to write:
Iterable<(K key, V value)> mapEntries<K, V>(Map<K, V> map) => map.entries.map((e) => (e.key, e.value));
The names are useful here.
We currently disallow names that would conflict with another field's name.
We don't allow (int foo, {int foo}) or (int, {int $1}) because those names would conflict with other names,
or even (int, int $1).
We do allow (int $1, int $2) because those name won't conflict with the name of another field.
So, all in all, the current behavior is consistent and by design.
If you don't want to write names for positional record fields, you can obviously choose to not do so.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
I'm sure there's a reason for not allowing it, but I would like to be able to access my positional parameters by name. My project has some "refactors" of removing classes like typedef _ChartData = (DateTime date, double value);
final _ChartData data = (/* */);
print(data.date) // should work imo
final class _ChartData {
const _ChartData(this.date, this.value);
final DateTime date;
final double value;
}
final _ChartData data = _ChartData(/* */);
print(data.date); // works fine |
Issue #3487 goes into this in some detail.
If it were me, I would consider keeping those classes. If you have a type that has a meaningful name, meaningful state, accessors for that state, and you want to write a constructor for it... that's a class. |
The contents of the class |
Whether the variable name in the tuple should be forbidden because it cannot be used, e.g. (double x, int a) should only be written as (double, int)?
(double, int $2) Is this writing also prohibited?
The text was updated successfully, but these errors were encountered: