zajrik / option_result

A lightweight Dart library for Rust-like Option/Result types and associated pattern matching
https://pub.dev/packages/option_result
MIT License
10 stars 1 forks source link

Only non-null values #6

Closed manuel-plavsic closed 7 months ago

manuel-plavsic commented 10 months ago

This pull requests ensures that nullable types cannot be used when working with Option or Result. Additionally, since refactoring was necessary anyway, catchOption, catchOptionAsync, catchResult and catchResultAsync were simplified so that only the value for the Some option and, respectively, the one for the Ok option must be returned.

It can be argumented that using Option with nullable types defeats its purpose. None should be used instead of using Some wrapping null. By enforcing this constraint, a lot of bugs can be prevented.

Also, consider the NI typedef below:

typedef NI = int?

void fn(NI? a) {
  final option = Option.from(a);
}

If a is null, then this could mean one of these things:

What Option values were expected and which ones were created?

At the Result/Ok level, having a non-nullable T (i.e. T extends Object) would bring only benefits. An Option<T> type should be defined instead of a nullable T?. No longer having to decide between Option<T> and nullable type T? will result in a much more uniform style.

It is fine for E to extend Object, because:

In conclusion, enforcing non-nullable types removes ambiguity and prevents bugs.

manuel-plavsic commented 10 months ago

I have just noticed that oxidized enforces non-nullable types as well.