getkyo / kyo

Toolkit for Scala Development
https://getkyo.io
Apache License 2.0
481 stars 38 forks source link

Maybe: allocationless Option (with nesting support!) #464

Closed fwbrasil closed 4 weeks ago

fwbrasil commented 4 weeks ago

The implementation matches the behavior of Scala's Option, even when there's nesting! The key insight is that nesting is only a problem when there are nested Some(None), which the implementation tracks via cached internal SomeNone instances. The only scenarios where this implementation allocates objects is when the nesting of Some(None) exceeds 100 (quite unlikely) and when Some.unapply is used. I had to use Scala's Option for pattern matching because the compiler isn't able to locate isDefined/get as extension methods to consider Some.unapply an extractor.

I'm a bit uneasy about naming this new impl Option since it can be confusing to users but we have a precedence with Duration.

fwbrasil commented 4 weeks ago

Renamed to Maybe