-
Notifications
You must be signed in to change notification settings - Fork 209
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
[Records] Should records have an asMap
method?
#2673
Comments
I had that same conversion with @rakudrama. :) An early version of this proposal had a static I'm not opposed to adding something like it back—I can see it being useful—but our general experience has been that any reflective API ends up being regretful so I'm hesitant to do it unless we know there's real demand for it. |
I'd prefer to not add such a method, but I'm less worried about performance. And, most importantly, there is no reification step in the other direction (you cannot go from an arbitrary map/list to a record). It really is just a synthetic method on each record shape, so Map<Object, Object?> toMap() => {0: $0, 1: $1, #x: x}; If we put an abstract method with that signature on |
I would certainly categorize If all these issues are negligible then we should obviously have an |
Is this a goal? This type of method was discussed early in the design, it was part of the original proposal and then we removed it. We cited performance concerns at the time.
I think you previously argued that this functionality would belong in In any case, I think I agree with Lasse that "I don't want to allow you to do any operation on an untyped record, other than checking that it has a specific known structure." |
I could be persuaded otherwise, but I think the right answer is to leave this out. |
I'm fine leaving this out, but I don't think this requires any metadata that isn't already required for the |
It does if it requires us to keep field names at runtime. We left |
I do think it belongs in It's not reflection in the most dangerous sense, one that requires keeping source names at runtime and allowing access through them (resolving dynamically created names at runtime). It does retain names, though, and structure information that could otherwise possibly be dropped. Other language features does that too, like I'd be happy to not make field access on records be normal instance methods, but more like extension/no-virtual methods on the static record type which accesses the fields directly, so that they cannot be accessed through Adding a That makes it one more way that that information will need to be retained in AoT builds. Which is a reason to avoid it. And it is specific to records, unlike (Reflection is very useful. People like it when it's there, like in Java. But it only really works in a JIT setting, it costs too much for AoT builds, sometimes in unpredictable ways, which is why Dart has been moving away from reflection on everything but the JIT'ed VM. We still have dynamic invocation and |
In discussion , @rakudrama suggested the possibility of specifying that records provide an
asMap
method, producing either aMap<String, Object?>
or perhaps aMap<Object, Object?>
if we prefer to use integers to index the positional fields.This would make writing generic code over records feasible in more places, e.g. we could make the
toString
method on records be a method onRecord
which used the asMap method + iteration.We could also consider providing an
asList
method which provided an integer indexed view (perhaps using a canonical order for the named parameters, or else just providing a view on the positional fields).cc @lrhn @munificent @eernstg @jakemac53 @natebosch @stereotype441 @kallentu @mit-mit
The text was updated successfully, but these errors were encountered: