amnaredo / test

0 stars 0 forks source link

Trouble pprint'ing #167

Open amnaredo opened 2 years ago

amnaredo commented 2 years ago

Consider e.g.

import pprint.Config.Defaults._

trait SomeTrait { }

case class A(s: String) extends SomeTrait
case class B(i: Int) extends SomeTrait
case class C(h: A, y: B, f: Map[String, Int]) extends SomeTrait

object ThingsHappen {
  def main(args: Array[String]) = {
    val a: SomeTrait = A("Yo")
    val b: SomeTrait = B(5)
    pprint.pprintln(a)
    pprint.pprintln(b)
    val c: SomeTrait = C(A("Yo"), B(5), Map("Hi" -> 5))
    pprint.pprintln(c)
  }
}

The situation I'm in is the above -- the situation I would like, result-wise, is if you remove those type declarations from 'a', 'b' and 'c' so that they are inferred as their respective case classes; this produces escaped strings etc. as expected.

I get why PPrint is not e.g. escaping strings in the above -- It's getting SomeTrait's, and it doesn't know how to PPrint that so its calling .toString on it, which gives you the normal result you'd get if you just called .toString on case classes.

What is the suggested way to solve this? Do I have to add something to each case class? Is there something I can do in SomeTrait that will solve this for all the case classes extending it?

ID: 145 Original Author: alathon

amnaredo commented 2 years ago

You forgot to seal your trait

haoyi-test@ {
            trait SomeTrait { }

            case class A(s: String) extends SomeTrait
            case class B(i: Int) extends SomeTrait
            case class C(h: A, y: B, f: Map[String, Int]) extends SomeTrait

            object ThingsHappen {
              def main(args: Array[String]) = {
                val a: SomeTrait = A("Yo")
                val b: SomeTrait = B(5)
                pprint.pprintln(a)
                pprint.pprintln(b)
                val c: SomeTrait = C(A("Yo"), B(5), Map("Hi" -> 5))
                pprint.pprintln(c)
              }
            }
            }
defined trait SomeTrait
defined class A
defined class B
defined class C
defined object ThingsHappen

haoyi-test@ ThingsHappen.main(null)
A(Yo)
B(5)
C(A(Yo),B(5),Map(Hi -> 5))

haoyi-test@ {
            sealed trait SomeTrait { }

            case class A(s: String) extends SomeTrait
            case class B(i: Int) extends SomeTrait
            case class C(h: A, y: B, f: Map[String, Int]) extends SomeTrait

            object ThingsHappen {
              def main(args: Array[String]) = {
                val a: SomeTrait = A("Yo")
                val b: SomeTrait = B(5)
                pprint.pprintln(a)
                pprint.pprintln(b)
                val c: SomeTrait = C(A("Yo"), B(5), Map("Hi" -> 5))
                pprint.pprintln(c)
              }
            }
            }
defined trait SomeTrait
defined class A
defined class B
defined class C
defined object ThingsHappen

haoyi-test@ ThingsHappen.main(null)
A("Yo")
B(5)
C(A("Yo"), B(5), Map("Hi" -> 5))

Original Author: lihaoyi