-
Notifications
You must be signed in to change notification settings - Fork 51
Description
In version 1.1.0 of fpdart:
The following code snippet is a simplified version of a piece of code that had a runtime error.
I assume, because I'm mapping over the List of testOption the Option.Do constructor does not catch the throwing of _OptionThrow.
I assumed that the exception would be caught by the do Constructor, but this was not the case.
Here is the problematic snippet:
test('test', () async {
Option<Iterable<String?>> testOption = const Option<List<String?>>.of(
['/', '/test', null],
);
Option<Iterable<Uri>> doNotation = Option.Do(
($) => $(testOption).map(
(String? stringValue) => $(
optionOf(
Uri.tryParse($(optionOf(stringValue))), //
),
),
),
);
expect(doNotation, equals(const Option<Uri>.none()));Here is the thrown exception:
Instance of '_OptionThrow'
package:fpdart/src/option.dart 45:28 _doAdapter.<fn>
package:fpdart/src/option.dart 589:72 None.match
package:fpdart/src/extension/option_extension.dart 29:39 FpdartOnOption.getOrElse
package:fpdart/src/option.dart 45:12 _doAdapter
test/implementation_test.dart 197:27 main.<fn>.<fn>.<fn>
dart:core _StringBase._interpolate
package:fpdart/src/option.dart 558:39 Some.toString
package:matcher expect
package:flutter_test/src/widget_tester.dart 454:18 expect
test/implementation_test.dart 202:5 main.<fn>
Here is a rewrite without a callback that correctly catches the thrown exception and returns none
test('test2', () async {
Option<Iterable<String?>> testOption = const Option<List<String?>>.of(
['/', '/test', null],
);
Option<Iterable<Uri>> doNotation = Option.Do(($) {
List<Uri> list = [];
for (final String? stringValue in $(testOption)) {
final uri = $(optionOf(Uri.tryParse($(optionOf(stringValue)))));
list.add(uri);
}
return list;
});
expect(doNotation, equals(const Option.none()));
});This is pretty dangerous, because I don't have the type safety at compile time but have a runtime error I totally oversaw in the code review.
How should I proceed with this?
Could the Do constructors implementation be fixed so that callbacks can be used inside, or is this a language limitation and I therefore can't use the Do constructor when Using callbacks (which would be hard, because mapping lists etc is not possible anymore)?
Thanks.