简体   繁体   中英

Matching Lscala.Tuple2 in scala

I am playing some with the Scala code inside of Spark's DAGScheduler and having issues matching what appears to be a Tuple2. Admittedly I don't know Scala very well so I am having some troubles. I have an object that appears to be a Tuple2 (at least that is what is reported when I use the getClass method). However, I have tried all manner of matching syntax to get this to match. The code I am trying looks like:

def verify(resultArray: ArrayBuffer[Any]) {
    logInfo("RESULTS STRING: "+resultArray(0).toString)
    logInfo("RESULTS CLASS: "+resultArray(0).getClass)

    resultArray(0) match {
      case Tuple2 =>
        logInfo("TUPLE2")
      case Tuple2(_, _) =>
        logInfo("TUPLE2(_, _)")
      case (v1, v2) =>
        logInfo("(v1, v2)")
      case (_, _) =>
        logInfo("(_, _)")
      case tu: (_, _) =>
        logInfo("tu: (_, _)")
      case (s: String, i: Int) =>
        logInfo("(s: String, i: Int)")
      case _ =>
        logInfo("_")
    }

More or less I tried to cram every possible match syntax I could find on the web for matching tuples into the match statement. However, none of these match. What gets printed:

14/04/28 03:37:32 INFO Verifier: RESULTS STRING: [Lscala.Tuple2;@4627d044
14/04/28 03:37:32 INFO Verifier: RESULTS CLASS: class [Lscala.Tuple2;
14/04/28 03:37:32 INFO Verifier: _

Does anyone have any idea what might be going on here? Or any tips?

So after the comments above I have implemented something like:

  def verify(resultArray: ArrayBuffer[Any]) {
    val loop = new Breaks
    var count = 0
    var votes:Array[Int] = new Array[Int](4)
    var i = 0
    logInfo("RESULTS STRING: "+resultArray(0).toString)
    logInfo("RESULTS CLASS: "+resultArray(0).getClass)

    resultArray(0) match {
      case Some(Int) =>
        logInfo("Some(Int)")
      case _ =>
        logInfo("_")
        resultArray(0).asInstanceOf[Array[Tuple2[_, _]]](0) match {
          case (_, _) =>
            logInfo("(_, _)")
            val tuples = resultArray(0).asInstanceOf[Array[Tuple2[_, _]]]
            for (t <- tuples) {
                logInfo("1: "+t._1+" 2: "+t._2)
            }
          case _ =>
            logInfo("result(0)(0): _")
        }
    }

Which seems to print out all the tuples in that array. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM