简体   繁体   中英

Scala type class for obtaining the most concrete type for a type member

Consider the following Scala code:

trait Elem
class MyElem extends Elem

trait Holder {
  type EP <: Elem
  def get: EP
}

class MyHolder(e: MyElem) extends Holder {
  type EP = MyElem
  def get = e
}

Depending on whether I have an object typed Holder or MyHolder , calling get may return an Elem or MyElem , respectively. I'm trying to make that information available at type level, but I'm not getting any results. Consider, for example, a Getter type class like this:

trait Getter[From] {
  type Out
  def get(from: From): Out
}

object Getter {
  type Aux[From, Out0] = Getter[From] { type Out = Out0 }

  def apply[From](implicit getter: Getter[From]): Getter.Aux[From, getter.Out] = getter

  // some way to create a `Getter[H <: Holder]`
  // where `Out` is the most specific type known for `H#EC`
}

Getter[MyHolder] // should return a MyHolder { type Out = MyElem }
Getter[Holder] // should return a Holder { type Out = Elem }

I'm trying to create the implicit constructor for Getter[H <: Holder] missing in the snippet but even though I tried several ways of constraining the types, I'm not getting any results that work for both examples above.

Is this possible to do with the Scala compiler? Does any type class from shapeless or scalaz help me do that?

What you're asking sounds like an application of Path Dependent Types

Wherein you can type something based on the type defined in some trait/class without knowing the concrete implementation:

val x: Holder#EP = ???

I hope this is what you're looking for, if not I'm afraid I don't fully understand your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM