I'm starting to learn Kotlin, and just noticed that Collections.isEmpty
is a function, rather than a property. However, Collections.size
is a property and not a function.
In most (all?) collections, I expect these two to be related semantically, and the implementation of isEmpty
can simply be size === 0
.
So... why isn't isEmpty
a property? Is it simply a design bug?
Using isEmpty() function The standard solution to check if a list is empty in Kotlin is with the isEmpty() library function. It returns true if the list contains no elements, false otherwise.
A collection is a group of related items, like a list of words, or a set of employee records. The collection can have the items ordered or unordered, and the items can be unique or not. You've already learned about one type of collection, lists. Lists have an order to the items, but the items don't have to be unique.
When deciding which methods of java.util.Collection
will be properties in kotlin.collections.Collection
the following considerations were taken into account:
size
and isEmpty
can be qualified as properties according to Kotlin coding conventions, however size
is inherent to the collection and isEmpty
is just a derivative of the size
property. size
val property with a var property (likely with a private setter), however the same is hardly ever needed for isEmpty
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With