简体   繁体   English

在scala中匹配Lscala.Tuple2

[英]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. 我正在Spark的DAGScheduler中使用Scala代码,但遇到与看似Tuple2匹配的问题。 Admittedly I don't know Scala very well so I am having some troubles. 诚然,我对Scala不太了解,所以遇到了一些麻烦。 I have an object that appears to be a Tuple2 (at least that is what is reported when I use the getClass method). 我有一个似乎是Tuple2的对象(至少这是我使用getClass方法时所报告的内容)。 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. 我或多或少地尝试填充所有可以在网络上找到的用于将元组匹配到match语句中的匹配语法。 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! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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