https://kotlinlang.org logo
Title
f

Florian

07/06/2019, 5:56 PM
in an if check,
in
is the same as
contains
. What is the operator function for
in
when we use it in a loop?
d

Dominaezzz

07/06/2019, 6:09 PM
It's a regular function called
iterator
.
f

Florian

07/06/2019, 6:36 PM
ok I thought operators always need an operator fun
d

Dico

07/06/2019, 6:40 PM
It's
operator fun iterator()
and the return type needs to conform to
operator fun hasNext(): Boolean
operator fun next(): T
Where T can be anything, including primitives and inline classes, which won't be subject to auto boxing. That would happen if you use the
Iterator
type though because of generics.
All these functions can also be extension functions. I wouldn't particularly recommend that in this case.
f

Florian

07/06/2019, 6:44 PM
thanks
for an Array it says
operator fun
for IntProgression it is a normal fun
is it because it overrides?
probably
d

Dico

07/06/2019, 6:52 PM
Yeah
f

Florian

07/06/2019, 6:53 PM
just to make that clear, the same operator is used for 2 completely different things
because the word "in" fits in both situations
k

karelpeeters

07/06/2019, 9:40 PM
Yes.
f

Florian

07/07/2019, 8:55 AM
ok cool
Why do the functions in many files have no body but the classes are not abstract
d

Dominaezzz

07/07/2019, 12:46 PM
Are they marked as
expect
?
f

Florian

07/07/2019, 1:19 PM
no
the stdlib is full with them
I think they get generated later or something?
maybe because they depend on the compilation target
I have to say I am not the most experienced dev, also not in Java
k

karelpeeters

07/07/2019, 3:07 PM
Can you give some examples so we can give a specific answer?
f

Florian

07/07/2019, 3:45 PM
yea if you go into Array.kt or Primitives.kt where Int, Long etc. are declared
the functions there are all empty
/ have no body
for example, I try to see the iterator() implementation for Arrays but I just end up at
public operator fun iterator(): Iterator<T>
d

Dico

07/07/2019, 4:08 PM
They're intrinsic
All primitives etc have that as well
f

Florian

07/07/2019, 4:09 PM
what does that mean exactly
where is it handled
when I step into an Array's iterator() function with the debugger, I get into ArrayIterator.kt
whereas Cltr + B brings me to Array.kt
but yea ArrayIterator.kt actually contains the code
d

Dico

07/07/2019, 4:12 PM
It's handled in the compiler
They are called built-in types
I think