튜플 유형을 스칼라의 해당 구성 요소 유형으로 분해 할 수 있습니까? ,스칼라에서 튜플 유형 풀기
type First
type Second
type Element = (First, Second)
def get(x: First): Second
튜플 유형을 스칼라의 해당 구성 요소 유형으로 분해 할 수 있습니까? ,스칼라에서 튜플 유형 풀기
type First
type Second
type Element = (First, Second)
def get(x: First): Second
이런 일이, 그 자체는,하지만 어쩌면 이것은 당신이 원하는 것을 달성 하지만 패턴 일치를 사용하는 것은 어떻습니까? 아주 정확한 반환 형식이없는, 나의 구문은 여기에 조금 떨어져, 그러나 수도 것은 간다 :
def get[K](key: K): Iterable[Any] {
for ((key, x) <- elements) yield x
}
나는이에 조금 늦게 해요 :
는 내 말은, 당신은 압축을 풀 수 없습니다
trait Container {
type Element
}
trait AssociativeContainer extends Container {
type Element <: (Unit, Unit)
def get(x : Element#First) : Element#Second
}
이 유형 압축을 해제하지 않습니다하지만 A
및 B
는 get
를 호출 유형을 제한 않습니다 . Element
추상적 인 경우 작동하지 않습니다 -
trait Container {
type Element
}
trait AssociativeContainer extends Container {
type Element <: Tuple2[_, _]
def get[A, B](x: A)(implicit ev: (A, B) =:= Element): B
}
이 유망 보이지만, 바람을 피우고있다.
def Unpack[T<:Tuple2[_, _]] = new {
def apply[A, B](implicit ev: T <:< (A, B)) = new {
type AA = A
type BB = B
}
}
trait AssociativeContainer {
type Element = (Int, String)
val unpacked = Unpack[Element].apply
type A = unpacked.AA
type B = unpacked.BB
1: A
"": B
def get(x: A): B
}
이것이 내가해야 할 필요가 있다고 생각했지만 피하고 싶었습니다. 왜냐하면 그것은 이러한 특성을 확장하는 클래스의 구현을 바꿀 것이기 때문입니다. – jpalecek
또한 요소 쌍이 하위 클래스에서도 동일한 쌍을 의미합니까? 그것은 오히려 요소 <:(첫째, 둘째) [또는 어쩌면 경계가 낮아야합니까?]가 아니어야합니까? – jpalecek