CharSequence extensions might return String or StringBuilder

Hi, is it expected behaviour that some of CharSequence extensions returns String while some StringBuilder?

for ex:

val s = "abc"
val s1 = s.substring(0, s.length).reversed()
val s2 = s.subSequence(0, s.length).reversed()
val s3 = s.subSequence(0, s.length).filter { true }
val s4 = s.subSequence(0, s.length)
val s5 = s.filter { true }
val s6 = s.subSequence(0, s.length).drop(1)

then log javaClass.simpleName:

s1 is String
s2 is StringBuilder
s3 is StringBuilder
s4 is String
s5 is String
s6 is String

Yes, operations on CharSequence are declared so that they produce another CharSequece. Both StringBuilder and String implement CharSequence, thus it’s ok to return either of them as the result.

1 Like