https://kotlinlang.org logo
Title
k

karelpeeters

09/20/2018, 6:58 PM
I don't like
repeat
when you actually use the index, it reads wrong to me. So I'd replace that with a for loop instead. I always like the
StringBuilder
/
apply
combo, so that's fine IMO.
s

Shawn

09/20/2018, 6:59 PM
curious that there isn’t a
buildString(String) { ... }
there’s one that accepts an int and a lambda, but it just uses the int to set the initial capacity
h

Hullaballoonatic

09/20/2018, 6:59 PM
i guess i could easily write one....
k

karelpeeters

09/20/2018, 7:36 PM
Turns out there is a
buildString
with exactly this behavior!
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/build-string.html
h

Hullaballoonatic

09/20/2018, 7:36 PM
oh my!
k

karelpeeters

09/20/2018, 7:37 PM
Ah wait you were talking about the prefix part, my bad I completely misunderstood what you were saying.
s

Shawn

09/20/2018, 7:37 PM
lmao no worries
k

karelpeeters

09/20/2018, 7:37 PM
Actually now I'm even more confused, what would you want
buildString(String) { ... }
to do?
s

Shawn

09/20/2018, 7:38 PM
StringBuilder(String)
takes a string to initialize the builder -
buildString
could do the same
k

karelpeeters

09/20/2018, 7:38 PM
Hmm I see.
h

Hullaballoonatic

09/22/2018, 1:50 AM
is there any advantage to stringbuilder over just string arithmetic like +?
s

Shawn

09/22/2018, 2:24 AM
mostly just the methods that the builder provides
they’re pretty convenient and, being named, they can be more descriptive, depending on the the code it’s being used in
if you’re building using a loop, then StringBuilder is a lot faster
when you’re just adding strings together, the StringBuilder and the
+
approach are the same - the compiler will transform the latter into the former
but the same optimization isn’t performed in a forEach or anything like that
(at least, I’m pretty certain it’s not 😅)