Skip to content

04 Printing to the console

Vladyslav Pekker edited this page Feb 2, 2017 · 14 revisions

⬅️ | ➡️

In the StylePrintModule spells shadows* methods print and println, originally defined in scala.Predef. These methods merely wrap their respective inputs in a call to styled (read more) before delegating to scala.Console, which scala.Predef would have done:

def println(input: Any = "")(implicit style: AnsiModule#AnsiStyle = AnsiStyle.Reset) = 
  Console.println(styled(input)(style))

This allows you to do the following:

scala> println("yellow " + "green".green + " yellow")(AnsiStyle.Yellow)
yellow green yellow

The StylePrintModule also contains the method printerr, which looks like this:

def printerr(error: Any): Unit = Console.err.println(erred(error))
def erred(error: Any): String = styled(error)(AnsiStyle.Red)

* Every time print or println are invoked with spells in scope, the ones defined in spells are called instead of the ones defined in scala.Predef.

⬅️ | ➡️

Clone this wiki locally