https://kotlinlang.org logo
Title
a

Aregev2

08/06/2018, 5:27 PM
What is the difference between
isEmpty()
and
none()
?
s

Shawn

08/06/2018, 5:28 PM
have you read the docs? they’re very different functions
assuming you’re talking about the methods defined on
Iterable
a

Aregev2

08/06/2018, 5:28 PM
yes
Not the methods with the predicates
But
none()
(not
none { }
)
a

adam-mcneilly

08/06/2018, 5:30 PM
Looks like they're similar, but the code seems slightly nuanced in that
isEmpty()
is on a collection but
none()
is on an
Iterable
specifically. I'm guessing it's possible for something to be iterable and not a collection and that's where you can use it?
a

Aregev2

08/06/2018, 5:31 PM
🤔 Interesting...
a

adam-mcneilly

08/06/2018, 5:33 PM
I'm guessing based on what I saw, I'm not claiming expertise here ha
I clicked into the source on
none()
a

Aregev2

08/06/2018, 5:33 PM
And..?
It just return isEmpty() 😕
a

adam-mcneilly

08/06/2018, 5:45 PM
only if it's of type collection
if (this is Collection) return isEmpty()
return !iterator().hasNext()
k

karelpeeters

08/06/2018, 6:28 PM
I don't really get why they added that method, they should have called it
isEmpty()
too.
Same for
find
and
firstOrNull
, just pick one.
☝️ 1
e

elye

08/07/2018, 6:47 AM
You could use
sequence.none()
but not
sequence.isEmpty()
. What is
sequence
? Check out https://medium.com/@elye.project/kotlin-slow-list-and-lazy-sequence-61691fc974c5
a

Andreas Sinz

08/07/2018, 3:05 PM
@karelpeeters I'd guess that they named it different because it behaves different.
Collection.isEmpty()
checks if there is anything inside the collection,
Iterator.none()
checks if any items are left to iterate over
k

karelpeeters

08/07/2018, 3:10 PM
No it's
Iterable.none
, you don't start with an existing iterator.
a

Andreas Sinz

08/07/2018, 3:12 PM
oh