How to print a list of strings to standard error in Scala?
18,339
Solution 1
Even not being able to exactly reproduce the problem, I do know you can solve the ambiguity by specifying the type:
scala> List("a","b","c")
res0: List[java.lang.String] = List(a, b, c)
scala> res0.foreach(System.err.println(_:String))
a
b
c
In this example the _:String
is unnecessary, it maybe necessary in your use case.
Solution 2
According to RosettaCode, calling the built-in Console
API is better than calling the Java runtime library with System.err
:
scala> List("aa", "bb", "cc").foreach(Console.err.println(_))
aa
bb
cc
Related videos on Youtube
Author by
ektrules
Updated on September 15, 2022Comments
-
ektrules over 1 year
This line causes a compile error:
astgen.typeError.foreach(System.err.println)
typeError is a scala.collection.immutable.List of Strings in the object astgen.
The error I'm getting is:
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)
I'm new to Scala and don't understand the problem. Using 2.7.7final.
-
pedrofurla over 11 yearsI could reproduce the problem in version 2.9.1. Which version are you using? Are you talking about scala's List or Java's?
-
soc over 11 yearsYou should consider upgrading, 2.7.7 is pre-historic. This issue has been fixed in a more recent version.
-
pedrofurla over 11 yearsOps, I meant I couldn't reproduce the problem in 2.9.1. And indeed @soc is right, 2.7.7 is paleolithic.
-
-
kap almost 7 yearsOnly
System.err
logs to stderr as Logback target.Console.err
does not work when specified as target, it then still logs to stdout.