gpt4 book ai didi

找不到 Scala : value x when unpacking returned tuple

转载 作者:行者123 更新时间:2023-12-04 13:51:34 25 4
gpt4 key购买 nike

我在无数网站上看到过这种代码,但似乎无法编译:

def foo(): (Int, Int) = {
(1, 2)
}

def main(args: Array[String]): Unit = {
val (I, O) = foo()
}

它在标记线上失败,报告:
  • 未找到:值 I
  • 未找到:值 O

  • 这可能是什么原因?

    最佳答案

    问题是使用了大写字母 IO在你的模式匹配中。您应该尝试将其替换为小写字母 val (i, o) = foo() . Scala Language Specification说明值定义可以扩展为模式匹配。例如定义 val x :: xs = mylist扩展为以下内容(参见第 39 页):

    val x$ = mylist match { case x :: xs => {x, xs} }
    val x = x$._1
    val xs = x$._2

    在您的情况下,值定义 val (i, o) = foo()以类似的方式展开。但是,语言规范还指出,模式匹配包含小写字母(参见第 114 页):

    A variable pattern x is a simple identifier which starts with a lower case letter.

    关于找不到 Scala : value x when unpacking returned tuple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16626052/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com