marcglasberg / fast_immutable_collections

Dart Package: Immutable lists, sets, maps, and multimaps, which are as fast as their native mutable counterparts. Extension methods and comparators for native Dart collections.
BSD 2-Clause "Simplified" License
218 stars 30 forks source link

fix types in sumBy to enable summing a list full of doubles #71

Closed matthias-prevost closed 8 months ago

matthias-prevost commented 8 months ago

What was the problem ?

When using sumBy on a list where the mapper function would return only doubles, the method throws an error. The error is :

type 'int' is not a subtype of type 'double' in type cast
package:fast_immutable_collections/src/base/iterable_extension.dart 139:18  FicIterableExtension.sumBy
test/base/iterable_extension_test.dart 30:38                                main.<fn>

Code in error :

  N sumBy<N extends num>(N Function(T element) mapper) {
    N result = 0 as N; // <- the code that throws the error
    ...
  }

Step to reproduce :

[1.5, 2.5, 3.3, 4.3, 5.3].sumBy((e) => e)

Reason : If the list only has double, type N is double and the type cast of 0 fails

marcglasberg commented 8 months ago

@matthias-prevost Thank you!

Published: version 10.1.2