Skip to content
Advertisement

Generic method to return function returning generic self-type without the need to cast

Honestly, I’m not even sure whether that title makes sense. Hopefully the code following will explain the issue at hand.

JavaScript

My issue is with the two methods applySimpleBound() and applyBoundWithGenericType(). The former is compiling fine, but needs explicit casting, which is what I’d like to get rid of. The later does not compile, because .apply(this) requires a type SELF but provided is ConcreteSelfType.

So my question is, how do I specify the signature of a method in SelfTypeTemplates to return an UnaryOperator<SELF> so that invoking the returned function (.apply(this)), does not need casting in the client code (i.e. ContreteSelfType)?

Tried to play with different bounds in the generic and return type. Haven’t found a working version without type casting.

Advertisement

Answer

Sometimes the compiler cannot infer the correct type for what ever reason. To work around this issue you can specify it like this:

JavaScript

Both options compile this way and you don’t need a cast.

Advertisement