amnaredo / test

0 stars 0 forks source link

Implicit PPrinters not identified #171

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

My build.sbt looks something like

scalaVersion := "2.11.8"
libraryDependencies += "com.lihaoyi" %% "pprint" % "0.4.0"

And then

scala> import pprint._
import pprint._

scala> PPrint(new Object())
<console>:15: error: type mismatch;
 found   : Object
 required: pprint.PPrinter[?]
       PPrint(new Object())
              ^

Am I missing something?

ID: 153 Original Author: samuela

amnaredo commented 3 years ago

Yeah you should be using

scala> pprint.pprintln(new Object)
java.lang.Object@1ec8ad64

PPrint it usually used as a typeclass to allow pretty-printing, e.g.

def log[T: PPrint](t: T) = {
  ...
  pprintln(t)
  ...
} 

The apply method on its companion is handy for constructing it, but generally shouldn't be used unless you're doing something special

Original Author: lihaoyi

amnaredo commented 3 years ago

Ah, ok. I was a bit confused because the website features an example that seems to use PPrint as a function:

scala> PPrint(new Object())
res35: String = java.lang.Object@54f880c0

How should I go about pretty printing something to a String instead of stdout?

Original Author: samuela

amnaredo commented 3 years ago

For that you can use pprint.tokenize.

If the docs are outdated I should go fix it...

Original Author: lihaoyi