8
한다고 가정 경계로 유형 매개 변수 또는 추상 유형을 사용하여 내가 가진 :스칼라 : 유형
class Bounded[A] {
type apply[C <: A] = C
}
이 컴파일 :
implicitly[Bounded[Any]#apply[String] =:= String]
이 실패
type Str = Bounded[Any]#apply[String]
...으로 :
[error] /home/grant/Workspace/scunits/test/src/main/scala/Box.scala:37: type arguments[String] do not conform to type apply's type parameter bounds [C <: A]
[error] type Str = Bounded[Any]#apply[String]
[error] ^
동일한 결과를 사용하여 유형 매개 변수 대신 추상 유형을 사용해 보았습니다. 유일한 해결책은 유형을 인스턴스화하는 것이 었습니다. 이것은 컴파일 :
val boundedAny = new Bounded[Any]
type Str2 = boundedAny.apply[String]
불행히도 나는 종종 성능상의 이유로 런타임 인스턴스가없는 팬텀 유형으로 작업하고 있습니다.
여기 왜 스칼라에서 컴파일 오류가 발생합니까? 더 나은 해결 방법이 있습니까?
도움 주셔서 감사합니다.
업데이트 : 아래의 해결 방법 외에도 추상 형식 경계로 형식을 재정의하는 방법이 필요했습니다. 그래서 같이 이런 짓을 :
object Test {
class AbstractBounded[A] {
type apply[C <: A] <: A
class Workaround[C <: A] {
type go = apply[C]
}
}
class Bounded[A] extends AbstractBounded[A] {
type apply[C <: A] = C
}
type Str = Bounded[Any]#Workaround[String]#go
}
감사합니다.이 버그는 저에게 많은 좌절의 원인이었습니다. – Grant