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
This issue proposes extending the language to support destructuring into class fields. This enhancement would enable developers to directly destructure and assign values from records or objects into corresponding class fields.
voidmain() {
int a =3;
int b =2;
MapEntry<int, int> entry =MapEntry(1, 2);
(a, b) = (1,2); // dart allows record destructuring into variablesMapEntry(key: a, value: b) = entry; // dart allows object destructuring into variablesprint('$a,$b');
}
classRecordDestructingExample {
// Error: dart does not allow record destructuring into fieldsRecordDestructingExample(): (a, b) = (1,2);
finalint a;
finalint b;
}
classObjectDestructingExample {
// Error: dart does not allow object destructuring into fieldsObjectDestructingExample(MapEntry<int, int> entry):MapEntry(key: a, value: b) = entry;
finalint a;
finalint b;
}
// dumb concrete example why this could be usefulclassExample {
// Error: dart does not allow record destructuring into fieldsExample(List<(int, int)> records): (a, b) =_getSum(records);
static (int, int) _getSum(List<(int, int)> records) {
int sumA =0;
int sumB =0;
for(final (a,b) in records) {
sumA += a;
sumB += b;
}
return (sumA, sumB);
}
finalint a;
finalint b;
}
The text was updated successfully, but these errors were encountered:
Not quite the same, but I'm certain that it we allow destructuring in constructor initializers, then we'll also allow it at field declarations and vice versa, so I'm going to go ahead and close this issue in favor of that one.
Uh oh!
There was an error while loading. Please reload this page.
This issue proposes extending the language to support destructuring into class fields. This enhancement would enable developers to directly destructure and assign values from records or objects into corresponding class fields.
The text was updated successfully, but these errors were encountered: