#1059 Is there a way to break out of, or go to the end of a block? (solved)

lel4866 Sun 4 Apr 2010

In Ruby, the next statement goes to the end of a functional block and break exits the block (they also have redo - meh). It seems in Fan that neither continue nor break are allowed.

While not a big deal, having these two constructs often eliminates quite a bit of indentation and, IMHO, makes code more readable.

Any other way of doing this?

Thanks

KevinKelley Sun 4 Apr 2010

Fantom's got break and continue for loops ( for and while ), but not blocks in general. Of course you can always write your block as a closure, then you can use return.

lel4866 Mon 5 Apr 2010

"return" appears to work as "break" for a block, which is great

It would really be nice to have some way of doing "continue"

tactics Mon 5 Apr 2010

I don't think continue really makes sense for blocks. The continue keyword skips an iteration of a loop, but blocks don't necessarily run more than once, and there's no easy way to quantify what qualifies as an "iteration" of a block.

We want to keep Fantom as simple as possible -- for developers, for tool writers, and for maintainers. There's all sorts of cool flow control mechanisms we can add to a language, but each one comes with the loss of some of that simplicity. (Remember, our target audience here are Java programmers, and many of them have never even seen a goto, iterator, or coroutine before).

But keep the ideas coming :)

lel4866 Wed 7 Apr 2010

Both these requests can be solved for Lists (main use case) with eachWhile:

[1,2,3,4].eachWhile |i| {
  if (i <= 2)
    return null // simulates continue, next
  echo("i=$i")
  return 1 // simulates break 
}

KevinKelley Wed 7 Apr 2010

Some related Fantom idioms (idia?)...

5.times |i| { ... }

(3..7).each |i| { ... }

and in #882, ivan showed an Iterable mixin; just define an appropriate eachWhile and you get the standard find, map, reduce, ... I could wish to have this added to the Range class.

Closures. Is there anything they can't do?

Login or Signup to reply.