2014-10-06 9 views
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 
} 

답변

1

방법에 대해 : 스칼라 별칭을 입력 매개 변수를 바인딩하기 전에 일반 (또는 다른 "추상적"유형)를 조회하는 것을 잊었다

scala> class Bounded[A] { class i[C <: A]{ type apply = C}} 
defined class Bounded 

scala> type TTT = Bounded[Any]#i[String]#apply 
defined type alias TTT 

scala> implicitly[TTT =:= String] 
res4: =:=[TTT,String] = <function1> 

. 감안할 때 = : = 잘 작동합니다 - 나를위한 버그처럼 보입니다. 어쩌면 암시가 다른 컴파일 레벨이나이 검사 직전에 해결 될 수 있습니다.

+0

감사합니다.이 버그는 저에게 많은 좌절의 원인이었습니다. – Grant