-
Notifications
You must be signed in to change notification settings - Fork 198
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
Is it possible to match a record to a map? Should it be? #2886
Comments
No. (To "is it possible". Probably also to "should it".) Records are not maps, nor vice-versa. Records are more like objects with a special implicit class per shape, and that type is not a map. You cannot create a record without writing the shape into the source, because if you don't, the compiler won't know the "class" of that record. You can treat records as objects, and use object patterns, like: if (decoded case FromMap(:foo)) {
print("Just the string $foo");
} but they're not map objects. Even if if (decoded case FromMap) { // nope, doesn't match matched, because it knows a fancy non-type-safe way to extract a record from a map, it wouldn't help. if (decoded case FromMap record) { // nope, doesn't match could work, but then we go back to the "fancy non-type-safe" operation to go from map to record. Presumably you'd want to use the string corresponding to the record field name to extract a value from the map. |
yeah I understand that we shouldn't keep source names in production code and it makes sense. However, we do need them when we do So I am wondering that when the compiler traverse the code and find a |
It's definitely possible, but again, matching source names to string values at runtime is two steps out of my comfort zone. A much more direct match would be matching an object to a record type, like: class Foo {
int foo = 0;
String bar = "";
}
typedef FooBar = ({int foo, String bar});
// ...
if (aFoo case FooBar r) { ... destructure object and build record ... } It's still mixin two concepts: Destructuring and re-structuring. Converting directly from one structure to another, through some implicit connection between their properties, is something I'd at least require an explicit declaration of, not something the compiler should just guess out of naming or types. |
the point is that we are already doing it here:
I seriously thought it was possible because the syntax between |
The problem is that the compiler can't tell which records might flow into which map patterns (or vice versa). In general, this kind of dataflow analysis for dead code elimination is really fragile and hard to make work well. Maps and records are very different constructs. They solve different problems and they have different trade-offs. That's why Dart has both map and record objects, and why it has map and record patterns.
The syntax for record types with named fields does look sort of like a map, which is unfortunate. But it's not because records are map-like. It's because we wanted record types to roughly mirror parameter lists, which use |
I'm going to close this because we don't have any plans to change this part of the language, but it's a good question. Thanks for asking. |
I am doing some tests and it seems that is currently not possible to match a
Record
against aMap
The following code produces
No
andtrue
:Will it be possible to retrieve a
Record
from aMap
?I can see packages exposing builders that do that and I was wondering if this should be possible from the core.
If you think it through
Map matching
is very close to it.The text was updated successfully, but these errors were encountered: