2015-02-04 3 views
2

이 코드를 작동시키는 데 문제가 있습니다. 나는 그것을 상속받은 클래스가 "자식"을 가질 수있는 특성을 만들고 싶지만, 분명히 ChildsetParent 메서드는 P을 원하지만 Parent[P, C]이 대신 발생합니다.주기적으로 참조 된 형질에 대한 스칼라 유형 오류

package net.fluffy8x.thsch.entity 

import scala.collection.mutable.Set 

trait Parent[P, C <: Child[C, P]] { 
    protected val children: Set[C] 
    def register(c: C) = { 
    children += c 
    c.setParent(this) // this doesn't compile 
    } 
} 

trait Child[C, P <: Parent[P, C]] { 
    protected var parent: P 
    def setParent(p: P) = parent = p 
} 

답변

5

당신은 thisP하지 Parent[P, C]을 나타 내기 위해 자기 유형을 사용해야합니다. 또한 여분의 범위가 필요합니다. P <: Parent[P, C]C <: Child[C, P]

trait Parent[P <: Parent[P, C], C <: Child[C, P]] { this: P => 
    protected val children: scala.collection.mutable.Set[C] 
    def register(c: C) = { 
    children += c 
    c.setParent(this) 
    } 
} 

trait Child[C <: Child[C, P], P <: Parent[P, C]] { this: C => 
    protected var parent: P 
    def setParent(p: P) = parent = p 
} 
+0

글쎄, 감사합니다! 나는 너에게 신용을 보장 할 것이다. – bb94