You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In his article "Structured Programming with go to Statements" Donald Knuth describes a syntactic pattern called "event indicators", which allow breaking out of a loop, while maintaining a value. They are similar in principles to exceptions, in roughly the same way that coroutines (Also suggested by Knuth) are similar to threads. From a modern perspective, there is also a parallel: A syntactic construct that creates a limited variant, while eliminating the need for run-time support.
The syntax is clearly distinguishable from a regular match due to the use of a label as a pattern.
While the above can also be implemented using a closure with enumeration return type, things become less trivial when dealing with cascaded indicators:
The above cannot be cleanly wrapped into closures, since you can't "return twice" in a single statement.
I also suggest that duplicate labels are allowed in the cascaded case, with the innermost matching label applying. In this case "matching" can also include overloaded parameters:
In the above, the most appropriate 'catch block is chosen based on the support of the From trait in each case. Only compile-time information is used, however, to determine the type of error returned from each method.
It differs from regular exceptions in that it is method-local, so there's no unwinding of the call stack.
The text was updated successfully, but these errors were encountered:
@glaebhoerl It's different from #961 because that one is limited to loops. #243 is actually a whole bunch of things, since it links to a rather long RFC, but the "Early exit from any block", with labelled returns, is similar. It's less expressive, though, since you need to clarify the block from which you want to exit, so overloading is not possible. For exception simulation, the implication is that you have to specify the catch-like-block that's supposed to capture the specific exception in the try macro.
@oli-obk As I've mentioned in the post, doing this using closures is only possible if you have only one layer. If you have a closure inside a closure, you can't force a break of the outer one from within the inner one. It also doesn't cover overloading. And you need to explicitly define an enumeration for each set of options with which you want to break.
nrc
added
the
T-lang
Relevant to the language team, which will review and decide on the RFC.
label
Aug 29, 2016
In his article "Structured Programming with go to Statements" Donald Knuth describes a syntactic pattern called "event indicators", which allow breaking out of a loop, while maintaining a value. They are similar in principles to exceptions, in roughly the same way that coroutines (Also suggested by Knuth) are similar to threads. From a modern perspective, there is also a parallel: A syntactic construct that creates a limited variant, while eliminating the need for run-time support.
The structure I suggest for this is as follows:
The syntax is clearly distinguishable from a regular match due to the use of a label as a pattern.
While the above can also be implemented using a closure with enumeration return type, things become less trivial when dealing with cascaded indicators:
The above cannot be cleanly wrapped into closures, since you can't "return twice" in a single statement.
I also suggest that duplicate labels are allowed in the cascaded case, with the innermost matching label applying. In this case "matching" can also include overloaded parameters:
The above should print
Hello world!
.Note that unlike a regular match, a catch-all pattern (_) is not necessary, since every label must be defined by the match patterns block.
It could also be used to simulate exception handling:
In the above, the most appropriate
'catch
block is chosen based on the support of theFrom
trait in each case. Only compile-time information is used, however, to determine the type of error returned from each method.It differs from regular exceptions in that it is method-local, so there's no unwinding of the call stack.
The text was updated successfully, but these errors were encountered: