Would there be any interest in adding some String ...
# language-evolution
d
Would there be any interest in adding some String interpolation syntax to automatically use array contents?
"array: ${arr.contentToString()}"
gets rather verbose. Perhaps
"array: $[arr]"
? (As an added bonus, the string builder could avoid the intermediate array string and add each element to the result
StringBuilder
directly, and the same syntax could be used to efficiently add strings from other iterables.) Or would the need to preserve backwards compatibility kill any attempt to add more features to String literals?
j
Not that I'm against such a proposal, but arrays are almost never used in Kotlin, apart from low level machinery. So adding convenience for arrays doesn't seem aligned with kotlin's philosophy
d
For the case of
Array<T>
, I agree, but there are still significant use cases for
IntArray
and the like so long as
Array<Int>
requires boxing.
j
Again, at low levels yes, but at the level of user presentation where you would care about convenient strong interpolation, not so much.
I might be wrong, though. Would you mind sharing your use case for high performance code that also needs string interpolation? What was the situation that made you think about this proposal?
d
My current use case is just debugging leetcode problems that often require using
Array<T>
,
IntArray
, etc. inputs and outputs. I expect this would be primarily useful for debugging code that needs the strong performance efficiency of non-boxed primitives. (Debugging with the debugger is usually better than print statements, but there are still many times where direct strings are quite useful.)