-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Case class toString method shows field names with values #6936
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
Conversation
THANK YOU SO MUCH |
re: testing, what you might do is add a partest case to case-class-toString.scala`: object Test extends App {
case class A(i: Int, s: String)
println(A(1, foo))
}
A(i=1, s=hello) |
Sorry, but I have to voice a negative vote on this one. This is going to break so much code, if only existing tests, that the convenience is never going to be worth it. If only we had done that while we could, like in 2.9.x or something. But now is too late. |
it might be worth opening a thread on contributors before doing more work, to see what the consensus of others is (if there is one) |
@sjrd Is the argument against ("is going to break so much code") from the standpoint that people out there parse the string output of case classes? Or is there some other consideration? |
Unit tests often make assertions like Similarly, some applications might be including Perhaps an annotation could be used to configure the generated |
Great if this were opt-in (and ideally per-field, but that might be too much to ask). You could add a (Which I agree with: I often feel the lack of "named args" in the built in |
Please continue the discussion about the idea behind this change in https://contributors.scala-lang.org/t/case-class-tostring-new-behavior-proposal-with-implementation/2056, rather than here. |
Redacted, moved comment to contributors discussion https://contributors.scala-lang.org/t/case-class-tostring-new-behavior-proposal-with-implementation/2056/29?u=olafurpg |
I reallllllly wish I could be in favor of this but with sadness and regret I lean towards @sjrd's position :-/ at least, I don't think we can simply merge this for 2.13 as-is :-/ :-/ :-/ perhaps we can continue to explore whether there's a way this could happen in stages. e.g. the "opt-back" proposal is interesting. |
@SethTisue do you think that there's any potential for a import scala.runtime.ScalaRunTime._toString
trait ClassicToString { this: Product =>
def toString = _toString(this)
}
case class A(i: Int, b: Int) extends ClassicToString ? |
Thank you for proposing and discussing this! I understand this is desirable, but we can't change this behavior anymore, at least not by default. I hope we can all rally behind #6972 to get that into M5 (I'm +1 on that one). I hope that will provide a good building block for a nicer, opt-in, case class toString, which is clearly on many wishlists. |
Currently, case classes have their
toString
method generated by the compiler, to print like this:This Pull Request adds the field names for more clear reading, so the above will now look like:
For inspiration, see Kotlin's data classes:
I have not yet added integration tests to this PR, as I'm not really sure how to set these up just yet.