No, I am not talking about performance. I am talking about code sanity.
Currently, anything, even null, Unit and Nothing (although the latter would trigger a “dead code” error), can be fitted into a string template. I think this is an error-prone behaviour.
When I use a variable in Kotlin, I already assume that the IDE/compiler would do type and null safety checks for me. While I am aware that this doesn’t include interpolation in string templates, I often accidentally use the absence of errors in a string template to assume that an expression is used correctly.
From what I have experienced (although I am not a very experienced Java/Kotlin developer), I don’t think the implicit toString call is useful. There are a few problems:
- While developers are advised to always override
toString,toStringdoes not always give the results you want.toStringis supposed to provide as much debugging data as possible, but this is not what a developer usually wants to do when typing a string template. In short, this behaviour is only useful for debugging. Indeed, in a properly i18nized project, string templates are almost never used for anything but debugging, but not every project is (or needs to be) i18nized. - On the other hand, this implicit
toStringbehaviour provides the motivation for developers to implementtoStringjust to “have more concise string templates”, e.g. as shortcuts to thename/idproperty, etc. While this is not a good practice anyway, it is tempting for new developers to do this (especially when they aren’t experienced with good testing or debugging techniques) - If only
Strings (or more tolerantly,CharSequences) are allowed, issues like Suggestion: Linting for String interpolation can be prevented as early as possible.
In this topic, I am proposing a new syntax for string literals that only accepts Strings/CharSequences. This can be something like changing the quotes to ''xxx'', or by introducing a new way of interpolating strings (but backward compatibility is a bit hard to solve). Or to make things simpler, apply a compiler flag that displays warnings when interpolating a nullable or non-CharSequence value in a string template.