help, any ideas how to implement this?
# announcements
i
help, any ideas how to implement this?
r
Can you edit this message and put everything else you posted here in it please? You’re writting in a channel with more than 25k people, we try not to spam
For your question, you named your function
repeatWhile
, it would see logical to me to use a function returning a Boolean instead
i
let me edit the name, this is outdated
How can I exit the loop?
r
if (!f()) break
Or
if (f()) break
, whatever
i
is there no way to be able to exit it similar to what I did?
the reason I don't want to use return is a bit discussable. But curious in any case
r
You can’t make a passed parameter know the label of a basic for-loop
Ok, you could use a more complex solution. You could define a class which would manage the loop. This class would be the receiver of
f
and have a function to stop the loop
i
the label isn't really the problem though. If I remove it it says "break" can't jump across a function or class boundary
hmm that might work
okay thanks. At leat I know now what's possible and what's not 🙂
btw it's not possible to force a return statement in a closure to apply only for the closure and not the enclosing function right?
m
☝️ 1
i
great thanks!
s
how about the function you want to retry returns if it was successful (and therefore doesn’t need further retries):
Copy code
fun retry(maxTimes: Int, f: () -> Boolean) {
    for (index in 0 until maxTimes) {
        val wasSuccessful = f()
        if(wasSuccessful) break
    }
}
i
I think this was already suggested in the thread, but I wanted to exit early in the block
d
@iex This is pure noise, no idea what you are even asking. Please explain your problems/questions better in future.
i
@darkmoon_uk did you expand the message? 🤔 not a poster-child question, but it seems fairly understandable.
I also find there are better ways to communicate what you're saying. "Pure noise" isn't accurate and unnecessarily confrontational.