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

YamlList needs to take type parameters? #36

Closed
eseidel opened this issue Jan 1, 2018 · 4 comments
Closed

YamlList needs to take type parameters? #36

eseidel opened this issue Jan 1, 2018 · 4 comments

Comments

@eseidel
Copy link

eseidel commented Jan 1, 2018

I'm attempting to make some of my code strong-mode clean.

For example:

    YamlList yamlItems = yamlMob['items'];
    if (yamlItems != null) {
      yamlItems.forEach(
          (String itemName) => mob.addItem(creator.items.itemByName(itemName)));
    }

Complains:

file: 'file:///Stuff/Projects/lol_duel/lib/utils/duel.dart'
severity: 'Info'
message: 'A function of type '(String) → Item' can't be assigned to a variable of type '(dynamic) → void'.'
at: '46,11'
source: 'dart'
code: 'uses_dynamic_as_bottom'

If this were a normal list, I'd fix it by using:
List<String> items

But I can't do that for YamlList, even thought it implements ListMixin which takes a type parameter:
abstract class ListMixin<E> implements List<E> {

@eseidel
Copy link
Author

eseidel commented Jan 1, 2018

I worked around this by using List<T> directly, since that's theoretically a superclass of YamlList?
eseidel/lolsim@2733e13
Not sure if that was the correct approach or if this will apply to others.

(This is not important, just me doing some fun hacking over the holidays.)

@nex3
Copy link
Member

nex3 commented Jan 3, 2018

This warning is correct—there's no guarantee that the elements of yamlItems are strings, so the callback you're passing may not be valid. I'd rewrite this as:

var yamlItems = yamlMob['items'];
if (yamlItems != null) {
  yamlItems.forEach(
      (itemName) => mob.addItem(creator.items.itemByName(itemName as String)));
}

@nex3 nex3 closed this as completed Jan 3, 2018
@eseidel
Copy link
Author

eseidel commented Jan 3, 2018

(Before your reply) I ended up writing it as:

    List<String> yamlItems = yamlMob['items'];
    if (yamlItems != null) {
      yamlItems.forEach(
          (String itemName) => mob.addItem(creator.items.itemByName(itemName)));
    }

I'm not sure if that's better or worse, but it seems to compile and run with the default options in at least the latest dart 2.0 devel brew release.

@eseidel
Copy link
Author

eseidel commented Jan 3, 2018

Nm, I had already written that above, my bad.

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