Comments (4)
Yeah, I agree this would be useful. As @tatumizer suggests, I do think it should be something you explicitly opt into, probably using the same ...
rest syntax we have for list patterns.
However, as @lrhn notes, it's challenging to incorporate this into refutable patterns. If we support:
var stuff = (a: 1, b: 2, c: 3);
var (:a, :c, ...) = stuff;
Then users would probably expect this to work:
var stuff = (a: 1, b: 2, c: 3);
if (stuff case (a: 1, :var c, ...)) {
print('a is one and c is $c');
}
But that might in turn lead them to expect this to work:
Object stuff = (a: 1, b: 2, c: 3); // <- Note: Has type Object now.
if (stuff case (a: 1, :var c, ...)) {
print('a is one and c is $c');
}
Even though this looks similar, it's a very different program. Because now, what is the type that stuff
is being tested against? Up to this point, every pattern is basically "test if the value has some type X and then do other stuff". But there's no type for "any record type that has these fields".
You can't just say "a record with fields a
and c
" because there's no subtype relationship between that record type and a record type like ({a: int, b: int, c: int})
. In the literature, this is called "width subtyping" and we deliberately avoided it in Dart because it makes optimizations and other stuff harder.
We could say that you can use incomplete record patterns (i.e. ones with a ...
) only in contexts where there is a surrounding context type that is a record type that fills in the ...
. That would work. I just worry that it would be a confusing limitation when users tried to use ...
in a place where there isn't that context type.
In short, it's an interesting idea and would be practically useful. I just worry about the risk of user confusion.
from language.
You can also use the defined type alias to object destructure the record. However, I do support this feature.
void main() {
final Big big = (field1: 42, field2: 42, field8: 42);
final Big(:field1, :field2) = big;
}
from language.
A straightforward solution would be to enable the syntax like
final (:field1, :field2, ...rest) = big;
final [x, y, ...rest] = [1, 2, 3, 4];
If the "rest" is of no interest, then
final (:field1, :field2, ..._) = big;
final [x, y, ..._] = [1, 2, 3, 4];
from language.
It's not completely unreasonable for a pattern with a record type as matched value type, to be able to ignore some of the fields.
The hurdle is that it's not allowed for a refutable match against a non-record type, because a record pattern must ensure that it is matched against a single known record shape.
That means case (: var x, ...)
is only allowed against some matched value types, as the only refutable pattern that can be invalid because of the type being matched.
from language.
Related Issues (20)
- Support paired-up private and public variables: "Private var, public final" HOT 3
- Let a wildcard as an actual type argument indicate a request for inference HOT 2
- Is future returned immediatelly from async call? Inconsistency in docs HOT 3
- Allow augmenting member declarations immediately in the same body? HOT 7
- [parts-with-imports] Consider dropping the library name from the library directive HOT 1
- Infer the type of an optional parameter from the default value
- Should digit separators be supported in int.fromEnvironment? HOT 1
- Support for Rust like #[cfg()] attributes HOT 10
- [parts-with-imports] Conditional part directives and the analyzer HOT 20
- Allow all variable augmentations to omit the initializing expression HOT 4
- `Error: Method not found` when running Flutter app with macro class HOT 2
- Allow a variable initializer to be unresolved, following the treatment of abstract members HOT 3
- Allow Iterators To Use The for-in Syntax HOT 5
- Macros: Emit diagnostics inside macro arguments HOT 2
- URI shorthands without internal whitespace.
- URI shorthands, allow reserved words.
- URI shorthands, more permissive HOT 1
- Inheriting a type parameter bound?
- Ordering of augmenting initializers and getters on late variables
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language.