You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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: ***@***.***>
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
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 _ChartData and replacing them with record typedefs, and we need to make the choice between making the record named, and updating the "constructors", or making them positional like the class, but updating the accessors to $1 or $2 when we should just be able to access it by name regardless. I'm sure there's an elaborate discussion in a separate issue on this subject but I can't find it right now.
typedef_ChartData= (DateTime date, double value);
final_ChartData data = (/* */);
print(data.date) // should work imofinalclass_ChartData {
const_ChartData(this.date, this.value);
finalDateTime date;
finaldouble value;
}
final_ChartData data =_ChartData(/* */);
print(data.date); // works fine
My project has some "refactors" of removing classes like _ChartData and replacing them with record typedefs
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.
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 _ChartData are listed in full above, it wasn't shortened for readibility. Considering that, I went with using named records and nothing else had to be changed, and now we have the benefit of implicit equality. :)
Activity
lrhn commentedon May 5, 2023
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:
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.
munificent commentedon May 5, 2023
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.
Silentdoer commentedon May 5, 2023
Reprevise commentedon Nov 13, 2023
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
_ChartData
and replacing them with recordtypedefs
, and we need to make the choice between making the record named, and updating the "constructors", or making them positional like the class, but updating the accessors to$1
or$2
when we should just be able to access it by name regardless. I'm sure there's an elaborate discussion in a separate issue on this subject but I can't find it right now.munificent commentedon Mar 11, 2024
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.
Reprevise commentedon Mar 11, 2024
The contents of the class
_ChartData
are listed in full above, it wasn't shortened for readibility. Considering that, I went with using named records and nothing else had to be changed, and now we have the benefit of implicit equality. :)