[HN Gopher] Thoughts on Return, Break and Continue
___________________________________________________________________
Thoughts on Return, Break and Continue
Author : lukastyrychtr
Score : 9 points
Date : 2022-01-07 15:32 UTC (7 hours ago)
(HTM) web link (blog.oberien.de)
(TXT) w3m dump (blog.oberien.de)
| coolgeek wrote:
| > Allowing breaking out of blocks, you don't need the continue
| keyword: '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,
| adamrezich wrote:
| one of the coolest features in Jon Blow's "jai" language is how,
| in lieu of making "iterators" a first-class language feature,
| instead you can define a "for_expansion" for your structs, which
| then lets you define what it means to iterate over your struct
| with a "for" loop. that's neat on its own but then you can
| actually redefine what "break", "continue", and "remove" (keyword
| that you can use for whatever you want but semantically it's
| supposed to be "remove item from collection during iteration in a
| safe way that doesn't affect the rest of the for loop"):
| Foo_Storage :: struct { items: [..] int;
| additional_info: string; } for_expansion ::
| (using storage: *Foo_Storage, body: Code, flags: For_Flags)
| #expand { for something: some_outer_loop {
| 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.
| Supermancho wrote:
| Python allows you to remove items as you iterate through them.
| Surprised it's such a rare feature.
| jdmichal wrote:
| The Java `Iterator` interface has a `remove` method, that in my
| experience is rarely used. Apparently rarely enough that it
| wasn't even a concern when hiding the `Iterator` instance in
| its foreach loop implementation.
|
| 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.
| politician wrote:
| How about just goto?
___________________________________________________________________
(page generated 2022-01-07 23:01 UTC)