2016-04-19 13 views
5

Mam pewne problemy z typami. W tym przypadku mam dwie cechy z metodami podstawowymi, a jedna z nich zależy od drugiej. Po tym mam dwie implementacje dla nich. Czy masz pojęcie, co jest nie tak?Cecha nie jest zgodna z ograniczeniami parametru typu

Kompilator mówi:

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] { 

Kod:

/* 
* 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 
} 
+0

Osobiście chciałem przyjrzeć i daje wskazówkę, ale don” Mam wystarczająco dużo czasu, żeby rozgryźć wszystko, co się tutaj dzieje. Masz 11 klas/cech, a to wyklucza dodatkowe typy. Będziesz miał znacznie większą szansę na dobrą reakcję, jeśli ograniczysz swój przykład do [MCVE] (http://stackoverflow.com/help/mcve). – slouc

+0

Już zrobiłem ten snipet i usunąłem wszystkie pozostałe części. Ale masz rację, wciąż jest ogromny. Nie jestem pewien, czy to pomaga, ale jeśli usuniesz klasę ImplOperations, to kompiluje. Problem polega na sprawdzeniu typów ChildOperarions. – rtfpessoa

Odpowiedz

4

problem wydaje się być podpis id def w ImplDefinition. Types.BaseT prosi o def id: Foo[I] ale ImplDefinition tylko dostarcza def id(a: ImplDto): Foo[ImplDtoIdentifier] jeśli dodać def id:Foo[ImplDtoIdentifier] = ??? do klasy rzeczy ImplDefinition skompiluje:

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