Higher Kinded Type Not Being Observed

112 views Asked by At

I'm doing something which seems like it should be fairly simple: trying to implement a typeclass defined for a collection with two type parameters, using a higher-kinded type as the type parameter for the typeclass.

The notable complexity here is that there is a type bound on the collection type.

I would expect that in the typeclass implementation body, the scala compiler should be able to infer the return type of methods based on the supplied typeclass type argument... Maybe this will be clearer in code:

import language.higherKinds   
class BaseDemo[T,Z]{
  def get(t:T):Z
}

trait BaseDempHKTC[BDType[_,_]<:BaseDemo[_,_]]{
  def doIt(arg:Int, bd:BDType[Int, BDType[Int, Double]) = {
    val out = bd.get(arg) //"type mismatch: expected T, actual Int"
    val outA:BDType[Int, Double] = out //nope. It thinks it's just a Z
  }
}

Is there a different pattern I should be using here? In my application, the Int and Double types are parameterized also as below, hence not just using the concrete types in the trait definition, but the above is the minimum to reproduce the error.

def doIt[TT,ZZ](arg:TT, bd:BDType[TT, BDType[TT, ZZ]) = ???
0

There are 0 answers