[英]How to print a list of strings to standard error in Scala?
这行会导致编译错误:
astgen.typeError.foreach(System.err.println)
typeError是对象astgen中的scala.collection.immutable.String列表。
我得到的错误是:
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
是不必要的,在您的用例中可能是必需的。
根据RosettaCode的说法 ,调用内置的Console
API优于使用System.err
调用Java运行时库:
scala> List("aa", "bb", "cc").foreach(Console.err.println(_))
aa
bb
cc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.