3 ms·
I agree with the author that having an option datatype makes the intention "this method returns some Foo or nothing" clear. However, the suggested encoding of
by limmeau 17y ago
I agree with the author that having an option datatype makes the intention "this method returns some Foo or nothing" clear.
However, the suggested encoding of an option type in Java blends poorly with subtyping because a JOption<MySubtype> is not a subtype of JOption<MySupertype>. For a quick optional-return scenario, it may work, but as a substitute for nulls in optional parameters, you'd have to remember the precise type of every parameter and create non-empty JOption instances in a rather verbose way.
As an alternative, you could use a tool like Findbugs and its @Nullable annotation.
- vladev 17y agoIn Scala's type system that can be expresses as well - it's called type variance. I believe C#4.0 might be getting this.
- limmeau 17y agoIn C#4.0, apparently you can declare JOption as interface NOption<out T> { ... } such that NOption<Mysub> is a subtype of NOption<Mysuper>. Still messy to read, though, and C# has ?-types anyway.