2009-02-23 9 views
7

タプル型をScalaのコンポーネントの型に分解できますか?私が意味するScalaのタプル型の解凍

、この

trait Container { 
    type Element 
} 

trait AssociativeContainer extends Container { 
    type Element <: (Unit, Unit) 
    def get(x : Element#First) : Element#Second 
} 

答えて

3

のようなものあなたはそれ自体が、解凍することはできませんが、多分これはあなたが望むものを実現:

type First 
    type Second 
    type Element = (First, Second) 
    def get(x: First): Second 
+0

これは私がする必要があると思っていましたが、避けたかったのは、この特性を拡張したクラスの実装が変更されるためです。 – jpalecek

+0

また、これはサブクラスでも要素ペアが同じペアになることを意味しますか?それはむしろ要素<:(最初、二番目)[またはあまりにも制限されていない]であるべきではありませんか? – jpalecek

0

私は少し遅れて、これによ、パターンマッチングの使用はどうですか?かなり正しい戻り値の型を持っていない、と私の構文が少しオフになる場合もありますが、ここに行く:

def get[K](key: K): Iterable[Any] { 
    for ((key, x) <- elements) yield x 
} 
3

これは種類を展開しませんが、getを呼び出すとき、それは種類ABを拘束しません。

trait Container { 
    type Element 
} 

trait AssociativeContainer extends Container { 
    type Element <: Tuple2[_, _] 

    def get[A, B](x: A)(implicit ev: (A, B) =:= Element): B 
} 

これは有望に見えるが、浮気された - Elementが抽象的であるならば、それは動作しません。

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 
}