Skip to content
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

YamlMap does not sport any type parameters #38

Closed
srawlins opened this issue Feb 23, 2018 · 6 comments
Closed

YamlMap does not sport any type parameters #38

srawlins opened this issue Feb 23, 2018 · 6 comments

Comments

@srawlins
Copy link
Member

This makes it hard to call forEach, or other methods that take a callback, without using dynamic as bottom (dart-lang/sdk#29630):

YamlMap a = ...
a.forEach((String k, YamlMap v) { ... }); // uses dynamic as bottom.
@srawlins
Copy link
Member Author

Same with YamlList.

@nex3
Copy link
Member

nex3 commented Feb 23, 2018

I don't know if there's much we can do about this... there are no type guarantees about what these values contain. YAML even allows non-string keys. Ultimately, you have to make an assertion at some point that the values are the types you expect, and Dart as a language strongly encourages you to do that explicitly with as or Map.cast().

@nex3 nex3 closed this as completed Feb 23, 2018
@srawlins
Copy link
Member Author

Can we not just do:

class YamlMap<K, V> extends YamlNode with collection.MapMixin<K, V>, UnmodifiableMapMixin

?

@nex3
Copy link
Member

nex3 commented Feb 23, 2018

Where would those type parameters ever be instantiated? YamlMap is only ever constructed by the YAML parser, which can make no guarantees about the heterogeneity of the objects the mapping contains.

@srawlins
Copy link
Member Author

I'm just looking for an implicit cast (another Dart language feature):

YamlMap<String, YamlMap> a = ...
a.forEach((String k, YamlMap v) { ... });

How would you suggest using Map.cast?

@nex3
Copy link
Member

nex3 commented Feb 23, 2018

Implicit/as casts on generic types like that are not runtime strong-mode safe unless the type is actually constructed with those generic types. It works in Dart 1 semantics, which had to be very lax around reified generics because the type system made it nearly impossible to guarantee that anything stayed reified, but in Dart 2 it will fail. That's why the cast() methods are being added: they change the reified type arguments that type-check values as they're accessed.

You should write something like:

var map = /* ... */.cast<String, YamlMap>();
map.forEach((key, value) { ... });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants