Open samuelgoto opened 6 years ago
Oh, and here's a couple fun ones that complicate everything: yield
and await
. They suspend the execution context, making standard functions impossible to use without implementing stackful coroutines under the hood for DSLs (which I expect to be a no-go, since generators are stackless coroutines).
This is a collection of alternatives on how to handle
break
andcontinue
.Read this first
There is a lot of context/background and ideas considered in these threads. Please read them first.
Introduction
continue
andbreak
are interesting cases because there are two valid uses:Whereas:
Here are some options to be explored / compared.
Alternatives
Disallow
Like kotlin, In this formulation, we would disallow
break
andcontinue
inside block params. This would lead into block params not supporting iterations or early returns. I believe this wouldn't corner ourselves, so could be a smart way to sequence things.Inline
Inspired by kotlin's inline functions, in this formulation, the semantics of
break
andcontinue
would be determined evaluating the result of inlining (metaphorically, not literally wrt performance) the entire function call (e.g. like a C macro works). For example:Whereas
Modifier
Inspired by kotlin's inline functions, in this formulation, we could make the semantics switch based on the declaraction of the function.
Call modifier
inspired by java's for, in this formulation the distinction in behavior would be done at all site. For example:
Standard Exceptions
One interesting approach here is to make
continue
andbreak
throw a special standard Exception (say, ContinueException and BreakException), which can then be re-thrown or not (and understood by the lexical blocks).Comes with challenges.
Labels
Another interesting approach here is to make
continue
andbreak
behave lexically, but explicit labels to mean the local ones. For examples:This pulls the responsibility to the user to know the distinction, which I'm not sure if the right trade-off.
NOTE(goto): kotlin is planning to include continue/break but for inline functions only, meaning that you can't do a foreach with continue/break.
NOTE(goto): would love to hear about alternatives here.