Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trait does not conform with type parameter bounds

Tags:

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
}
like image 276
rtfpessoa Avatar asked Apr 19 '16 19:04

rtfpessoa


1 Answers

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] = ???
}
like image 73
lapislazuli Avatar answered Sep 28 '22 01:09

lapislazuli