runeflobakk/motif

Add upcast operation to Optional

runeflobakk opened this issue · 1 comments

It is sometimes useful to upcast an Optional to a less specific type

Say if your method is declared to return an Optional<A>, and your method prepares a value v of type B extends A. The statement return optional(v) will not compile, since the type is inferred to be Optional<B>

Candidate method names:

  • .as(Class<N super V>): Optional<N>
  • .upcastTo(Class<N super V>): Optional<N>

Not possible...
http://stackoverflow.com/questions/4902723/why-cant-a-java-type-parameter-have-a-lower-bound
Basically due to Java being oriented towards mutability, and has use-site variance.
If only Java generics had declaration-site variance... http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Generic_types

This also prevents #37 from being implemented.