https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jonathan Ellis

05/24/2022, 7:04 PM
is there some heuristic whereby it makes sense that collections.last() is a function but size is a property?
e

ephemient

05/24/2022, 7:06 PM
j

Jonathan Ellis

05/24/2022, 7:08 PM
thanks!
e

ephemient

05/24/2022, 7:09 PM
I think there are some parts of stdlib where it's arguably inconsistent - e.g.
isEmpty()
- but lots of parts of Kotlin pre-date the modern style document
2
y

Youssef Shoaib [MOD]

05/24/2022, 11:07 PM
I was attempting to justify in my mind why
isEmpty
would be a method, but nope I can't find any reason. It doesn't do any expensive or long computation in the case of normal collections (just a size check) or Iterables/Sequences (just a
hasNext
) check. Sure, hasNext theoretically could be "expensive", but I don't think that justifies it. ... wait, why isn't hasNext a property? 🤦
e

ephemient

05/25/2022, 12:15 AM
arguably,
hasNext()
can be expensive, e.g.
Copy code
generateSequence(2.toBigInteger()) { it.nextProbablePrime() }.iterator().hasNext()
but that doesn't apply to
isEmpty()
since that's a method on
Collection
and other types with a
size