Anoying problem, but what else can be done?
Don Clugston
dac at nospam.com.au
Mon Feb 5 01:24:06 PST 2007
Frits van Bommel wrote:
> BCS wrote:
>> this fails
>>
>> foreach(i, j; Tuple!(1,2,3,4)
>> {
>> loopLable: while(test())
>> {
>> while(otherTest())
>> {
>> if(lastTest())
>> break loopLabel;
>> else
>> continue loopLabel;
>> }
>> }
>> }
>>
>> The reason is that loopLabel is used once for each iteration of the
>> foreach, which is unrolled. Seeing as labels have functions scope,
>> this is an error.
>>
> [snip "Duff's Device"-like workaround]
>>
>> A better solution would be to define that labels have function scope
>> from a syntactic standpoint, meaning that the first code would be
>> valid because, while the labels references more than one location in
>> the final program, it only occurs onces in the code. This would be
>> somewhat bad but only because you can't jump from one iteration to the
>> next (IIRC the switch/case/goto case can).
>
> I'd like this to work, but for any identifiers (labels, variables,
> aliases, whatever). Whatever is declared in a 'static' foreach body
> should be local to that iteration of it, because AFAICT it's pretty much
> guaranteed to be an error or bug otherwise.
NO! Definitely not true. Suppose you want to create an alias for the
first 'int' item in a tuple. You don't know which iteration will create
the alias.
I've made use of the current behaviour, even with labels. It allows you
to write things like:
foreach(...) {
static if (somecomplexcondition) {
LabelX:
...
}
static if(someothercondition) {
goto LabelX;
}
}
You can create a state machine in this way; you need to be able to jump
from one foreach iteration into another one.
More information about the Digitalmars-d
mailing list