2017-10-01 2 views
0

내가 자주 다음과 같은 문제가 발생하고 다음 AuctionParticipantActorAkka 배우와의 분산이 올바른가요? 제네릭 및 Akka 배우를 사용하는 경우

trait AuctionParticipantActor[P <: AuctionParticipant[P]] 
    extends StackableActor { 


    override def receive: Receive = { 
    case message: Handled => 
     participant = participant.handle(message) 
     super.receive(message) 
    case message => 
     super.receive(message) 
    } 

    protected var participant: P 

} 

것은 불변의 AuctionParticipant 주변 단지 래퍼입니다. 나는 타입이 P이 공변 (covariant)해야하고 이것을 성취하는 가장 좋은 방법이 무엇인지 모르겠습니다.

또는 내 사용 사례에 대해서는 AuctionParticipantActor을 매개 변수화 할 필요가 없다고 생각합니다. 나는 다음과 같은 것을 가질 수있다 :

trait AuctionParticipantActor 
    extends StackableActor { 


    override def receive: Receive = { 
    case message: Handled => 
     participant = participant.handle(message) 
     super.receive(message) 
    case message => 
     super.receive(message) 
    } 

    protected var participant: AuctionParticipant[???] 

} 

그러나이 경우에, 나는 무엇 대신 넣을 지 모르겠다. 바인딩 된 타입을 존중하기 위해서. 누군가 내 문제가 디자인에 있다고 생각한다면 그렇게 말하십시오. 생각?

답변

0

f-bounded-polymorphism을 사용하지 않으면 왜 AuctionParticipant을 제네릭으로 사용해야합니까? AuctionParticipant[P]에 유형 매개 변수 P의 의미는 무엇입니까? 만약 당신이 말했듯이, 은 AuctionParticipant 이상의 래퍼 일 뿐이며 AuctionParticipantActor은 더 이상 일반 적이 지 않다면 AuctionParticipant도 안됩니다.

trait AuctionParticipantActor 
    extends StackableActor { 


    override def receive: Receive = { 
    case message: Handled => 
     participant = participant.handle(message) 
     super.receive(message) 
    case message => 
     super.receive(message) 
    } 

    protected var participant: AuctionParticipant 

} 

trait AuctionParticipant { 
    // ... 
} 

그렇지 않으면 AuctionParticipant 경우 여전히 (즉, P의 다른 의미가있다) 어쩌면 당신이 사용할 수있는 일반해야 existential type :

trait AuctionParticipantActor 
    extends StackableActor { 


    override def receive: Receive = { 
    case message: Handled => 
     participant = participant.handle(message) 
     super.receive(message) 
    case message => 
     super.receive(message) 
    } 

    protected var participant: AuctionParticipant[_] 

} 

trait AuctionParticipant[P] { 
    // ... 
} 
+0

명확하게하기 위해, 나는 AuctionParticipant'에 ​​대한 F-경계 다형성이 필요 'trait (이 특성의 메서드는 하위 클래스를 반환합니다.) 그렇기 때문에 실재 형식 매개 변수는 형식 범위를 위반하므로 컴파일되지 않을 것이라고 생각합니다. – davidrpugh

+0

@davidrpugh 당신은 'AuctionParticipant'의 정의를 작성하지 않았습니다. 나는 당신이 당신의 질문을 업데이트해야한다고 생각합니다. –

+0

@davidrpugh 존재 유형이 다를 수 있습니다. 필요한 경우 AuctionParticipant [_ <: UpperBound] 또는 'AuctionParticipant [_> : LowerBound]'가 될 수 있습니다. 'protected var participant : AuctionParticipant [_ <: AuctionParticipant [_]]는 어떻습니까? 그것은 컴파일합니까? –

관련 문제