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

Add final modifiers #2

Closed manuel-plavsic closed 7 months ago

manuel-plavsic commented 10 months ago

First of all, great package!

I don't think it makes sense to extend the classes that are affected by this PR. It would make more sense to create custom sealed classes, for example, for Err types, e.g.:

sealed class CustomError {
  // ..
}

final class Possibility1 extends CustomError {
  // ..
}

final class Possibility2 extends CustomError {
  // ..
}

Result <int, CustomError> myFn() {
  // ..
}

Also, this greatly improves pattern matching. Consider the following non-final class:

class ExampleClass {}

Before this PR the IDE does not highlight potentially unmatchable types, due to Dart's implicit interfaces. For example, there could be a class that extends ExampleClass and implements Option:

Screenshot from 2023-11-19 13-11-15

After this PR:

Screenshot from 2023-11-19 13-12-41