경로에 대한 typeclass가 있습니다. 여기에는 확대 메소드 (경로가 정점을 중심으로 확대되어 확대됨)가 포함되어 있습니다.이 메소드는 경로 유형 클래스에 다시 부합하는 모든 유형을 반환 할 수 있어야합니다. 나는 다음과 같은 오류 얻을 그러나메서드에서 typeclass를 사용하는 개체 반환
case class Circle()
case class Polyline()
trait Path[T] {
/// Note how the return type T2 should again have a Path[T2] typeclass
def enlarge[T2 : Path](path : T) : T2
}
object Path {
implicit object PathCircle extends Path[Circle] {
def enlarge[T2 : Path](path : Circle) = Polyline()
}
implicit object PathPolyline extends Path[Polyline] {
def enlarge[T2 : Path](path : Polyline) = Polyline()
}
}
object Test {
import Path._
implicitly[Path[Circle]].enlarge(Circle())
}
:
[error] test.scala:12: type mismatch;
[error] found : Polyline
[error] required: T2
[error] Note: implicit object PathPolyline is not applicable here because it comes after the application point and it lacks an explicit result type
[error] def enlarge[T2 : Path](path : Circle) = Polyline()
[error] ^
[error] test.scala:15: type mismatch;
[error] found : Polyline
[error] required: T2
[error] def enlarge[T2 : Path](path : Polyline) = Polyline()
[error] ^
[error] test.scala:22: ambiguous implicit values:
[error] both object PathPolyline in object Path of type Path.PathPolyline.type
[error] and object PathCircle in object Path of type Path.PathCircle.type
[error] match expected type Path[T2]
[error] implicitly[Path[Circle]].enlarge(Circle())
[error] ^
[error] three errors found
[error] (compile:compile) Compilation failed
내가 무슨 일을하고있는 중이 야 나는 이런 일이 (단순 제거 데이터 멤버)를 작동합니다 생각?
발신자가 반환 유형을 선택하게합니다 (예 : PathCircle.enlarge [Circle] (Circle())을 호출하면 어떻게 될까요? –
현재 확대/축소 방법에서 반환 된 폴리 라인을 사용자 지정 유형 (귀하의 경우 Circle)으로 변환 할 수있는 Conv [T] typeclass가 있습니다. 그러나 이것이 올바른 방법이 아니라는 것이 옳습니다. 어쩌면 반환 형식에 Path Typeclass가 있어야한다고 지정하는 다른 방법이 있을까요? – MHOOO