scala - 如何在Scala中将字符串列表打印为标准错误?

标签 scala io

此行导致编译错误:

astgen.typeError.foreach(System.err.println)

typeError 是对象 astgen 中的 scala.collection.immutable.List 字符串。

我得到的错误是:
error: ambiguous reference to overloaded definition,
both method println in class PrintStream of type (java.lang.String)Unit
and  method println in class PrintStream of type (Array[Char])Unit
match expected type (Nothing) => Unit
      astgen.typeError.foreach(System.err.println)

我是 Scala 的新手,不明白这个问题。使用 2.7.7final。

最佳答案

即使无法准确重现问题,我也知道您可以通过指定类型来解决歧义:

scala> List("a","b","c")
res0: List[java.lang.String] = List(a, b, c)

scala> res0.foreach(System.err.println(_:String))
a
b
c

在这个例子中 _:String是不必要的,在您的用例中可能有必要。

关于scala - 如何在Scala中将字符串列表打印为标准错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12888455/

相关文章:

java - Scanner(System.in)-如何取消/跳过输入等待

c# - 在 .NET 中创建固定大小的文件

haskell 如何从此代码中删除列表用法?

python - python中打开文件I/O内存管理

Scala,如何在一行中读取多个整数并将它们分别放入一个变量中?

eclipse - 将 Scala 项目从 github 导入 Eclipse

eclipse - 如何获取Scala中的项目路径?

scala - 如何在spark中为diff文件名调用单独的逻辑

scala - 如何通过ZIO环境在ZIO任务之间共享ZIO队列

java - 使用 Java 进行序列化 I/O