Is there an advantage to doing something like ```...
# announcements
b
Is there an advantage to doing something like
Copy code
val Strings.empty by lazy { "" }
and using
Strings.empty
where ever an empty string is needed in code (eg
val a = b ?: Strings.empty
)?
s
not even remotely for the specific case of the empty string
☝🏻 2
but I imagine you might have a more complex use case in mind
b
nope just came across this in existing code. I assume it's to prevent extra allocations, but it seems like something that the compiler ought to do or does already.
s
For sure, especially with literals
☝🏻 1
that
lazy
invocation is actually hampering the compiler’s ability to just inline the value where needed
I guess that may or may not be something you want in very specific conditions, but you’re probably just better off making string constants
const val
instead of anything fancy
or, just, y’know, use the empty string literal in your code because that’s a pretty acceptable case of using a literal (assuming you won’t later want to replace what the default value if null should be)
s
Kotlin also has String?.orEmpty() ¯\_(ツ)_/¯
👍 5
3