runeflobakk / motif

A Java library helping you to compose behavior from smaller parts.
http://runeflobakk.github.io/motif
6 stars 0 forks source link

Add upcast operation to Optional #36

Closed runeflobakk closed 10 years ago

runeflobakk commented 10 years ago

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:

runeflobakk commented 10 years ago

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.