Thoughts on Return, Break and Continue
blog.oberien.de
blog.oberien.de
Foo_Storage :: struct {
items: [..] int;
some_other_data_structure: Bar_Storage;
}
for_expansion :: (using storage: *Foo_Storage, body: Code, flags: For_Flags) #expand {
for something: some_other_data_structure {
for `it, `it_index: items {
#insert (remove=#assert(false), break=break something) body;
}
}
}
foos: Foo_Storage;
for foos { /*...*/ }
I've never seen anything like this in any other language and I've found it to be an incredibly useful tool to have at your disposal.Otherwise, this seems like a case of operator overloading. Sure, as long as `continue` and `break` do something like they're supposed to, it works. But how often is that true, when it's also NOT true that simply implementing something like an `Iterator` interface with a fixed implementation of those operators? If the only advantage is in providing the ability to break expectations, that's probably just a bad idea.
Which is why I said, it seems like the only advantage is the ability to break expectations. Which is arguably, realistically a disadvantage.
Compare that to e.g. Swift's iteration, with corners of undefined behaviors, potential allocations, and a collection types hierarchy that feels more like doing taxonomy than just looping.
Though to be fair, Jai's loop is rather intense in its usage of a macro system's features.
Related: Common Lisp loop macro: http://www.ai.sri.com/pkarp/loop.html
'loop: loop 'block: {
// continue
break 'block;
// break
break 'loop;
}
I get the intellectual appeal of experimentation, self-imposed constraints, minimizing instruction sets, etc.But forcing block labeling in order to avoid the necessity of continue statements seems like a wrong turn,
I mean yes, in the end, control blocks and break/continue statements are just special cases of jumps. But the innovation was in the other direction: Discovering that the vast majority of jumps can be classified into a small number of categories - ifs, switches, loops, break, continue...
The amazing thing is that you can even restrict your language to allow only jumps from those categories - and you will not lose expressiveness but your programs will suddenly become significantly easier to analyse.