Closed
Description
Alternative to the current syntax for labeled scopes, that would be beneficial if combined with #4285 (AConf literals).
Below are examples from the documentation written with the alternative seqblk based syntax:
test "labeled break from labeled block expression" {
var y: i32 = 123;
const x = seqblk blk {
y += 1;
break blk, y; // label and value arguments always present
};
// ...
}
test "nested break" {
while (true) seqblk outer {
while (true) {
break outer, _ ; // allow `_` when no value is returned from the block
}
}
// ...
}
test "nested continue" {
var i: usize = 0;
while (i < 10) : (i += 1) seqblk outer {
while (true) {
continue outer;
// `continue _ ;` to continue from innermost loop
}
}
// ...
}
test "while else" {
assert(rangeHasNumber(0, 10, 5));
assert(!rangeHasNumber(0, 10, 15));
}
fn rangeHasNumber(begin: usize, end: usize, number: usize) bool {
var i = begin;
return while (i < end) : (i += 1) {
if (i == number) {
break _, true; // `_` as first arg means break from innermost scope
}
} else false;
}
const success = seqblk x {
var i: usize = 0;
while (i < n) : (i += 1) {
if (found(i))
break x, true;
}
break x, false;
};In combination with #4285 (AConf literals), it becomes easy to add configuration options to sequential blocks. For example .{.constantTime = true} to take a use case from #1776 .
In short:
< seqblk <blkname>? <<AConf literal> : >? > { <statement>* }
where <AConf literal> would be of the form .{ <.config=.ConfigOption>* }
The seqblk keyword would only be used when you need either a label and/or a different configuration for a scope, so in most cases code wouldn't need changing with this proposal.
seqblk blk .{.runtimeSafety = .OFF, .blocking = false} : {
// ... statements here
}
while(cond) seqblk outer { // only use label, default config
while(cond2) seqblk .{.runtimeSafety =.OFF} : { // no label, change config
// ...
if(somecond){
continue outer;
}
}
}
Reacted by Felix Queißner
Activity
ikskuh commentedon Feb 10, 2020
I really like the approach of having a
break-Operator that takes one operand for "break loop" and two operands for "return value from block".But i would remove the option for "implicit block break" with
break _, …as it's even hard to recognize what will be broken in your code examples above.