简体   繁体   中英

Trait does not conform with type parameter bounds

I am having some issues with types. In this case I have two traits with base methods, and one of them depends on the other. After that I have two implementations for them. Do you have any idea what is wrong here?

The compiler is saying:

type arguments [ImplDefinition,ImplDto,ImplDtoIdentifier] do not conform to trait ChildOperations's type parameter bounds [C <: Types.BaseT[A,I],A <: Types.IDObj[I],I <: IIdentifier]
[error] class ImplOperations extends Parent2(new ImplDefinition) with ChildOperations[ImplDefinition, ImplDto, ImplDtoIdentifier] {

The code:

/*
 * Generic Implementation
 */

object Types {

  type IDObj[I <: IIdentifier] = AnyRef {def id: I}

  type BaseT[A, I <: IIdentifier] = Parent1[A] {
    def id: Foo[I]
  }

}

trait IIdentifier extends Any {
  def id: Long

  override def toString = id.toString
}

class Parent1[A](a: String)

class Foo[A](a: String)

trait ChildDefinition[A <: Types.IDObj[I], I <: IIdentifier] {
  self: Parent1[A] =>

  def id(a: A): Foo[I]

}

class Parent2[A](a: A)

trait ChildOperations[C <: Types.BaseT[A, I], A <: Types.IDObj[I], I <: IIdentifier] {
  self: Parent2[C] =>

  def get(identifier: I): Option[A]
}

/*
 * Concrete Implementation
 */

case class ImplDtoIdentifier(id: Long) extends IIdentifier

case class ImplDto(id: ImplDtoIdentifier, name: String)

class ImplDefinition extends Parent1[ImplDto]("Value") with ChildDefinition[ImplDto, ImplDtoIdentifier] {
  override def id(a: ImplDto): Foo[ImplDtoIdentifier] = ???
}

class ImplOperations extends Parent2(new ImplDefinition) with ChildOperations[ImplDefinition, ImplDto, ImplDtoIdentifier] {
  self =>
  override def get(identifier: ImplDtoIdentifier): Option[ImplDto] = ??? // here I will use the id method from ImplDefinition
}

the problem seems to be the signature of the id def in ImplDefinition . Types.BaseT asks for a def id: Foo[I] but ImplDefinition only provides a def id(a: ImplDto): Foo[ImplDtoIdentifier] if you add a def id:Foo[ImplDtoIdentifier] = ??? to the ImplDefinition class things will compile:

class ImplDefinition extends Parent1[ImplDto]("Value") with ChildDefinition[ImplDto, ImplDtoIdentifier] {
  def id:Foo[ImplDtoIdentifier] = ???
  override def id(a: ImplDto): Foo[ImplDtoIdentifier] = ???
}

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