Scala模式匹配返回期望的类型,但之后会生成类型不匹配

皮革

我有以下scala对象定义:

case class RecursiveSettings (
    var recursiveFrom: Int,
    var recursiveTo: Int,
    var nonRecursiveFrom: Int,
    var nonRecursiveTo: Int,
    var betweenReach: Int,
    var scoresamephrase: Boolean
    )

我正在尝试从ArrayBuffer中获取变量:

import scala.collection.mutable.ArrayBuffer

def main(args: Array[String]){
    var settings = List("","1", "2", "0", "0", true)
    var newsettings = new ArrayBuffer[Any]

    println(settings)

    settings.foreach {a =>
        val z = a match{
            case "" => 0
            case s : String => s.toInt
            case _  => a
        }
        newsettings += z
    }
    println(newsettings)

    var result = new RecursiveSettings(0,0,0,0,0,true)
    println(result)

    for (i <- 0 to (newsettings.length - 1)){
        println("newsettings_i", newsettings(i))

        val x = newsettings(i) match{
            case y : Int => y
            case y : Boolean => y
            case _ => 0
        }
        println("x:", x)
        i match{
            case 0 => result.recursiveFrom = x
            case 1 => result.recursiveTo = x
            case 2 => result.nonRecursiveFrom = x
            case 3 => result.nonRecursiveTo = x
            case 4 => result.betweenReach = x
            case 5 => result.scoresamephrase = x
        }
    }
}

如果我注释掉i match语句并进行简单的类型匹配:

for (i <- 0 to (newsettings.length - 1)){
        println("newsettings_i", newsettings(i))
        val x = newsettings(i) match{
            case y : Int => "Int"
            case y : Boolean => "Bool"
            case _ => 0
        }
        println("x:", x)

代码编译,运行,我得到:

List(, 1, 2, 0, 0, true)
ArrayBuffer(0, 1, 2, 0, 0, true)
RecursiveSettings(0,0,0,0,0,true)
(newsettings_i,0)
(x:,Int)
(newsettings_i,1)
(x:,Int)
(newsettings_i,2)
(x:,Int)
(newsettings_i,0)
(x:,Int)
(newsettings_i,0)
(x:,Int)
(newsettings_i,true)
(x:,Bool)

但是,当我重新添加i match语句时,会收到很多此类投诉:

~/match.scala:44: error: type mismatch;
found   : AnyVal
required: Int
            case 0 => result.recursiveFrom = x

有人可以帮我了解一下:

  1. 为什么简单类型匹配会产生所需的结果,但是并没有传递给对象?

  2. 我该怎么做才能更正我的代码?

在此先感谢您,这让我动了好几个小时!

编辑

好的,因此,基于@Alex Savitsky和@Jakub Zalas的信息(非常感谢),我已经对原始代码进行了实质性修改,以使我希望它可以在功能上更加面向对象,从而可以处理混合的init值类型:

object matcher2{
def main(args: Array[String]):Unit = {

    val init = Array("",1, "4", null, "0", false)
    matchf(init)
}
def matchf(args : Array[_] ) : RecursiveSettings = {
    val settings : RecursiveSettings = args.map{
        case "" => 0
        case "true" => true
        case "false" => false
        case b : Boolean => b
        case s : String => s.toInt
        case i : Int => i
        case null => 0

    } match {
        case Array(recursiveFrom: Int, recursiveTo: Int, nonRecursiveFrom: Int, nonRecursiveTo: Int, betweenReach: Int, scoresamephrase: Boolean) =>
        RecursiveSettings(recursiveFrom, recursiveTo, nonRecursiveFrom, nonRecursiveTo, betweenReach, scoresamephrase)
    }
    println(settings)
    settings
}
}

作为Python的Scala(和Java)的新手,我仍然在功能和静态类型方面苦苦挣扎,因此,任何评论/建议都深表感谢。

谢谢你的帮助。

亚历克斯·萨维茨基

您可以动态地将参数转换为适当的类型,然后一次匹配整个集合:

// assuming your args is an array of ["", "1", "2", "0", "0", "true"]
val settings: RecursiveSettings = args.map {
    case "" => 0
    case "true" => true
    case "false" => false
    case s: String => s.toInt
} match {
    case Array(recursiveFrom: Int, recursiveTo: Int, nonRecursiveFrom: Int, nonRecursiveTo: Int, betweenReach: Int, scoresamephrase: Boolean) =>
        RecursiveSettings(recursiveFrom, recursiveTo, nonRecursiveFrom, nonRecursiveTo, betweenReach, scoresamephrase)
}

您还可以对部分提供的参数进行匹配,只要您决定哪些参数将接收默认值即可(这种情况可以与“全”大小写匹配一起使用):

    case Array(recursiveFrom: Int, recursiveTo: Int) =>
        RecursiveSettings(recursiveFrom, recursiveTo, 0, 2, 1, true)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章