Rob Elliot
11/30/2020, 8:31 AMAnimesh Sahu
11/30/2020, 9:10 AMRob Elliot
11/30/2020, 9:40 AMlistOf("one", "two", "three").toString() == """listOf("one", "two", "three")"""
mapOf(1 to "one", 2 to "two").toString() == """mapOf(1 to "one", 2 to "two")"""
Person(name="Joe \"Awesome\" Smith", age=32).toString() == """Person(name="Joe \"Awesome\" Smith", age=32)"""
So that I could just cut and paste the output into a source file and it would compile.
Impossible now, would be a huge breaking change. Just venting a mild frustration as I yet again struggled to reproduce an issue from a log file and had to massage strings and so on to make the output compilable.Animesh Sahu
11/30/2020, 9:47 AMImpossible nowNot really, data classes output is directly compilable. And for lists and maps, you can define your own custom implementation:
fun <T> List<T>.toCompilableString(): String =
joinToString(", ", prefix = "listOf(", postfix = ")") { if (it is String) """"$it"""" else "$it" }
Rob Elliot
11/30/2020, 9:48 AMdata classes output is directly compilable.Not it isn’t. It doesn’t quote strings.
Rob Elliot
11/30/2020, 9:49 AMRob Elliot
11/30/2020, 9:50 AMPerson(name="Joe \"Awesome\" Smith", age=32).toString() == Person(name=Joe "Awesome" Smith, age=32)
- which is not compilable.Animesh Sahu
11/30/2020, 9:51 AMAnimesh Sahu
11/30/2020, 9:51 AMRob Elliot
11/30/2020, 9:52 AMAnimesh Sahu
11/30/2020, 9:53 AMVampire
11/30/2020, 9:57 AMtoString()
is for nice human consumption.
A compilable string is usually not fulfilling this point.
So I highly doubt this will ever change.Rob Elliot
11/30/2020, 9:58 AMVampire
11/30/2020, 9:58 AMRob Elliot
11/30/2020, 10:00 AMVampire
11/30/2020, 10:03 AMRob Elliot
11/30/2020, 10:08 AMRob Elliot
11/30/2020, 10:17 AM:
rather than to
between key and value in a Map entry - I really think that, apart from breaking backwards compatibility, it would be no big deal to human legibility if that changed
2) Strings are not quoted appropriately.
This is actually an issue for human readability too; you cannot reliably distinguish whether a value is a number or a boolean or a string. And even if you can, sorting out the escaping to get a compilable string in a map containing loads of them is a huge pain. For me the lack of ambiguity, and the ease of reuse, if the string were printed as a compilable string literal would significantly outweigh the reduction in legibility when part of the output of a list, map or data class.
(In my current role I quite often get a log of a Map<String, Any> which was parsed from json or some other format, unknowable at compile time, with fields like id: 1234
. The json isn’t logged. I’m trying to reproduce an issue by feeding the real world input into a test. Should I make that an int or a string? With work I can find out, but it would be a lot easier if the output were unambiguous in the first place.)Vampire
11/30/2020, 10:55 AMRob Elliot
11/30/2020, 10:59 AMVampire
11/30/2020, 11:00 AMAnimesh Sahu
11/30/2020, 11:00 AMVampire
11/30/2020, 11:00 AMchristophsturm
11/30/2020, 11:02 AMVampire
11/30/2020, 11:03 AMTobias Berger
11/30/2020, 12:04 PM