Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An interface to define custom pretty print to any objects? #35

Open
ndac-todoroki opened this issue Nov 19, 2020 · 2 comments
Open

An interface to define custom pretty print to any objects? #35

ndac-todoroki opened this issue Nov 19, 2020 · 2 comments

Comments

@ndac-todoroki
Copy link

ndac-todoroki commented Nov 19, 2020

Sometimes I want to print custom outputs from certain classes.

data class Foo(val key: Int, val value: List<Int>) {
   override fun toString(): String {    // pp() doesn't use this!
      return "#Foo($key => $value)"
   }
}

I wish I can override what pp() uses to format class instances, for example by implementing some interface.
(overriding fun pp() is an option too, but I think it is not the best, because the implementer needs not to forget newlines)

@derlin
Copy link

derlin commented Oct 10, 2021

What do you think of defining an annotation, @ppAtomic, that would instruct pp to call toString on instances of this class ?

@snowe2010
Copy link
Owner

Hm. The main point of pretty-print is to avoid the toString method, because toString methods usually do not do a good job of rendering the actual structure. Once dipping down into the toString, there's then no way to jump back out to rendering things 'correctly' (like the List in this example), without either duplicating things, or messing up the formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants