2012-04-17 11 views
1

matchSeqに対して書かれた理由はIndexedSeqタイプではLinearSeqタイプとは異なる動作をしますか?私には、以下のコードが入力の種類に関係なく正確に同じことをする必要があるようです。もちろん、そうでないか、私は尋ねていないでしょう。最終2.9.1でこれを実行するMatchedErrorはLinearSeqではなくIndexedSeqを受け取ります

import collection.immutable.LinearSeq 
object vectorMatch { 
    def main(args: Array[String]) { 
    doIt(Seq(1,2,3,4,7), Seq(1,4,6,9)) 
    doIt(List(1,2,3,4,7), List(1,4,6,9)) 
    doIt(LinearSeq(1,2,3,4,7), LinearSeq(1,4,6,9)) 
    doIt(IndexedSeq(1,2,3,4,7), IndexedSeq(1,4,6,9)) 
    doIt(Vector(1,2,3,4,7), Vector(1,4,6,9)) 
    } 

    def doIt(a: Seq[Long], b: Seq[Long]) { 
    try { 
     println("OK! " + m(a, b)) 
    } 
    catch { 
     case ex: Exception => println("m(%s, %s) failed with %s".format(a, b, ex)) 
    } 
    } 

    @annotation.tailrec 
    def m(a: Seq[Long], b: Seq[Long]): Seq[Long] = { 
    a match { 
     case Nil => b 
     case firstA :: moreA => b match { 
     case Nil => a 
     case firstB :: moreB if (firstB < firstA) => m(moreA, b) 
     case firstB :: moreB if (firstB > firstA) => m(a, moreB) 
     case firstB :: moreB if (firstB == firstA) => m(moreA, moreB) 
     case _ => throw new Exception("Got here: a: " + a + " b: " + b) 
     } 
    } 
    } 
} 

、私は次の出力を得る:

OK! List(2, 3, 4, 7) 
OK! List(2, 3, 4, 7) 
OK! List(2, 3, 4, 7) 
m(Vector(1, 2, 3, 4, 7), Vector(1, 4, 6, 9)) failed with scala.MatchError: Vector(1, 2, 3, 4, 7) (of class scala.collection.immutable.Vector) 
m(Vector(1, 2, 3, 4, 7), Vector(1, 4, 6, 9)) failed with scala.MatchError: Vector(1, 2, 3, 4, 7) (of class scala.collection.immutable.Vector) 

を、リスト-yのもののために細かい動作しますが、ベクトル-yのもののために失敗しました。何か不足していますか?これはコンパイラのバグですか?あなたはList以外の目的::を使用することはできません

@scala.annotation.tailrec def m(a: Seq, b: Seq): Seq = { 
    <synthetic> val _$this: object vectorMatch = vectorMatch.this; 
    _m(_$this,a,b){ 
    <synthetic> val temp6: Seq = a; 
    if (immutable.this.Nil.==(temp6)) 
     { 
     b 
     } 
    else 
     if (temp6.$isInstanceOf[scala.collection.immutable.::]()) 
     { 
      <synthetic> val temp8: scala.collection.immutable.:: = temp6.$asInstanceOf[scala.collection.immutable.::](); 
      <synthetic> val temp9: Long = scala.Long.unbox(temp8.hd$1()); 
      <synthetic> val temp10: List = temp8.tl$1(); 
      val firstA$1: Long = temp9; 
      val moreA: List = temp10; 
      { 
      <synthetic> val temp1: Seq = b; 
      if (immutable.this.Nil.==(temp1)) 
       { 
       a 
       } 
      else 
       if (temp1.$isInstanceOf[scala.collection.immutable.::]()) 
       { 
        <synthetic> val temp3: scala.collection.immutable.:: = temp1.$asInstanceOf[scala.collection.immutable.::](); 
        <synthetic> val temp4: Long = scala.Long.unbox(temp3.hd$1()); 
        <synthetic> val temp5: List = temp3.tl$1(); 
        val firstB: Long = temp4; 
        if (vectorMatch.this.gd1$1(firstB, firstA$1)) 
        body%11(firstB){ 
         _m(vectorMatch.this, moreA, b) 
        } 
        else 
        { 
         val firstB: Long = temp4; 
         val moreB: List = temp5; 
         if (vectorMatch.this.gd2$1(firstB, moreB, firstA$1)) 
         body%21(firstB,moreB){ 
          _m(vectorMatch.this, a, moreB) 
         } 
         else 
         { 
          val firstB: Long = temp4; 
          val moreB: List = temp5; 
          if (vectorMatch.this.gd3$1(firstB, moreB, firstA$1)) 
          body%31(firstB,moreB){ 
           _m(vectorMatch.this, moreA, moreB) 
          } 
          else 
          { 
           body%41(){ 
           throw new java.lang.Exception("Got here: a: ".+(a).+(" b: ").+(b)) 
           } 
          } 
         } 
        } 
       } 
       else 
       { 
        body%41() 
       } 
      } 

     } 
     else 
     throw new MatchError(temp6) 
    } 
}; 

答えて

14

mため

scalac -print出力は次のようになります。 ::Listを拡張するケースクラスであるため、Vectorは一致しません。そのため、unapplyメソッドはVectorでは機能しません。

val a :: b = List(1,2,3) // fine 
val a :: b = Vector(1,2,3) // error 

しかし、あなたはすべてのシーケンスのために働く、独自の抽出を定義することができます。

object +: { 
    def unapply[T](s: Seq[T]) = 
    s.headOption.map(head => (head, s.tail)) 
} 

だから、あなたが行うことができます答えを

val a +: b = List(1,2,3) // fine 
val a +: b = Vector(1,2,3) // fine 
+0

感謝を。 –

+0

ありがとう、@dhg。この回答を一度読んだら、これを読んで(http://www.scala-lang.org/old/node/112)、あなたの投稿をもう一度読んでください。 –

+0

ここで既に指摘したように(https://stackoverflow.com/a/11503173/1421535)、 'Seq'を' Seq(a、b、rest @ _ *) 'として使うこともできます。 – acidghost

関連する問題