https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jacques Smuts

04/05/2019, 1:36 PM
continue_vs_return.kt
Does it bother anyone else that repeat(x) doesn’t count as a loop so you have to use
return
instead of `continue`/`break`?
r

ribesg

04/05/2019, 1:38 PM
No.
1
Do you use
break
or
continue
in
forEach
?
j

Jacques Smuts

04/05/2019, 1:39 PM
no because I can’t, but it would make sense to me to do so
though I understand that it’s not technically a loop
r

ribesg

04/05/2019, 1:40 PM
Well it doesn’t for me because you should know that you’re using a lambda, which has a much bigger cost than a normal loop
👍 1
l

louiscad

04/05/2019, 2:07 PM
Well,
repeat
is inline and doesn't really cost more than a
for
or
while
loop
☝️ 1
But the semantics are different.
repeat
is a just a higher order function (a function that takes another one, called a lambda), while
for
,
while
, `do`/`while` are part of the control flow, similar to `if`/`else`, but with the added fact that they can loop.
j

Jacques Smuts

04/05/2019, 2:09 PM
so doesn’t that imply by Gael’s logic that
continue
should be applicable?
l

louiscad

04/05/2019, 2:10 PM
No,
continue
works only for raw control flow constructs like
while
j

Jacques Smuts

04/05/2019, 2:11 PM
That does make sense logically
l

louiscad

04/05/2019, 2:11 PM
skipping code is not possible. In a lambda, you can only run, return or throw. returning to the lambda works the same as
continue
though, so in practice, there's not much difference
Well, skipping code is possible with
if
, but you can't jump out of a lambda out of nowhere, unless that lambda is inline and you return to the function it's inlined in.
j

Jacques Smuts

04/05/2019, 2:15 PM
thanks for clarifying
3 Views