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.
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 nestedSome(None)
, which the implementation tracks via cached internalSomeNone
instances. The only scenarios where this implementation allocates objects is when the nesting ofSome(None)
exceeds100
(quite unlikely) and whenSome.unapply
is used. I had to use Scala'sOption
for pattern matching because the compiler isn't able to locateisDefined
/get
as extension methods to considerSome.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 withDuration
.