without looking at the docs, guess what this does:...
# advent-of-code
m
without looking at the docs, guess what this does:
Copy code
val packetVersion = stringbuilder.removeRange(0..2).toLong(2)
I guessed wrong this morning 😉
e
Did you want to do
substring
?
m
No, I expected that
removeRange(0..2)
on a stringbuilder, removes those elements from that stringbuilder and then returns them. instead, removeRange on a stringbuilder creates a whole new stringbuilder with everything but the chars at that range.
e
I see. That’s a rough part of Kotlin APIs, indeed, when you work with mutable types.
m
but that could just be me on a very early morning though 🧐
was used to the
removeFirst
-esque functions of mutable collections and thought
removeRange
worked the same, since stringbuilder is a mutablestring
should I make a youtrack with
removeAtRange
? 👀
e
It’s not the only inconsistently named function when you look at mutable/immutable things in stdlib. Consistently cleaning up the naming at this point maybe already too late, but there’s a chance we can fix most of those in a different way by proper IDE inspections and/or compiler warnings. However, I don’t see a good solution for this particular case of
removeRange
. Moreover, there’s a whole family of those extensions on `CharSequence`:
removePrefix
,
removeSuffix
,
removeSurrounding
that return a new
CharSequence
just like
removeRange
, which makes them confusing when applied to
StringBuilder
. because you might expect them to work similarly to `removeFirst`/`removeLast`/`removeAll`/etc on mutable lists.
e
the difference between CharSequence and StringBuilder functions has bitten me before too. I also don't see much that can be done about it, though.
m
I think one of the namings could be
excludeRange
excludePrefix
, etc. and then rewrite the removes, but that seems like hindsight. but
removeAtRange(...)
doesn't exist on any mutable collection, which could technically still be added for all of them
🤔 1
or
removeInRange
if that sounds better
j
Or copy off ruby and make it
removeRange!
😛