https://kotlinlang.org logo
Title
r

Robert

02/05/2019, 9:27 PM
Why does a `StringBuilder`have a method
append
but no method
prepend
, that does not make much sense to me. Anyone knows why there's none?
r

Ruckus

02/05/2019, 9:31 PM
Because you can use
insert(0, ...)
(or any other index for that matter)
a

alex

02/05/2019, 9:32 PM
There is `Insert`: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#insert(int,%20java.lang.String) But yeah, maybe there should be some sugar. Keep in mind, that prepending is expensive operation, unlike appending.
r

Robert

02/05/2019, 9:34 PM
There is none, but that's probably because I am doing MPP? 😞
a

alex

02/05/2019, 9:37 PM
It's also available for JVM:
typealias StringBuilder = StringBuilder
👍 1
but yes, nothing else except JVM and native
r

Robert

02/05/2019, 9:39 PM
I'll give it a try in #multiplatform Tyvm
k

kd

02/05/2019, 9:53 PM
You can probably write an extension function if you plan to use prepend a lot
r

Robert

02/05/2019, 11:04 PM
@kd IntelliJ suggested that, but I can only do that in the files itself (so the dependencies), which are read only 😞
d

Dico

02/06/2019, 5:35 AM
It's because it would be very inefficient implementation wise to support it
1
The most common use case of StringBuilder is to avoid concatenation of many strings, creating many intermediate steps
It's most efficient to put them in an array from left to right and not have to move them around
j

Jay

02/08/2019, 10:19 AM
There’s no reason why a circular buffer can’t be used, which means prepending or appending should both be equally cheap operations. Only inserting in the middle would be “expensive”. Even if it’s not implemented that way currently, the performance cost of a specific implementation should not be a good reason to cripple an API design.
👌 1
d

Dico

02/08/2019, 10:58 AM
StringBuilder just isn't intended to be used like that. You can do it though with insert. The vast majority of use cases can simply be expressed using appends, which makes it a StringBuilder, not a CharacterList. For a silly analogy - you don't move half of a house over after you've built it to make space for the other.
It's just a buffer that has improved repeated concatenation performance because it is not required to be immutable
r

Robert

02/08/2019, 9:24 PM
StringBuilder just isn't intended to be used like that.
How do you figure?
You can do it though with insert.
Problem is, there is no insert..
For a silly analogy - you don't move half of a house over after you've built it to make space for the other.
That's a strange analogy imho, which is not about the use case; we want to prepend, that's not the discussion. If the implementation for 'insert'/'prepend' is to move the house, then maybe the implementation is inefficient, but I agree with @Jay that it is strange to leave it out because of that. What about reverse + append + reverse? Even more inefficient I'd think?
d

Dico

02/08/2019, 11:20 PM
There is insert
I agree that they could have made the implementation such that operations other than append and replace would theoretically perform a bit better
But what I'm saying is I think they didn't because it is not the target use case
If you're in a situation when you need such control of a String you're making, and the performance of insert is too bad, it might be a good idea to make a StringBuilder that is tailored to your use case.