Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labels and "Control flow commands not allowed in toplevel" (Possible LTA error message)

Tags:

raku

This code:

FOO:
"Hey".say;
for <1 2> { 
    say "1";
    last FOO
}

Prints

Hey
1
Control flow commands not allowed in toplevel

The last error message is eliminated if "Hey".say is taken off; this probably means that what the error is actually saying is that non control flow commands are not allowed right behind a label. But the documentation (which needs to be improved cites loops as an "example", and the grammar indicates it should be in front of a statement. So the question is: can it be used for if statements, or just loops?

like image 954
jjmerelo Avatar asked Feb 15 '19 09:02

jjmerelo


1 Answers

The error is different if you have this code inside another scope:

{
    FOO:
    "Hey".say;
    for <a b> {
        .say;
        last FOO
    }
}
# Hey
# a
# labeled last without loop construct

which is also LTA in that it doesn't mention the name of the label.

But in short: Perl 6 doesn't currently have a goto. Currently, labels are only usable as a way to specify which loop construct you want to next, redo to or last out of. By putting a statement between the label and the loop construct, you're effectively using it as a goto, which is still Not Yet Implemented.

But indeed a ticket about the LTAness of both errors, seems in place for me.

Also, using the FIRST phaser as an alternative, seems to have the same issue:

FOO:
for <a b> { 
    FIRST "Hey".say;
    .say;
    last FOO;
}   
# Hey
# a
# labeled last without loop construct

But runs fine without the specific label:

FOO:
for <a b> { 
    FIRST "Hey".say;
    .say;
    last;
}   
# Hey
# a

The latter issue most definitely is worth a rakudo issue: added as https://github.com/rakudo/rakudo/issues/2699 .

like image 86
Elizabeth Mattijsen Avatar answered Oct 08 '22 03:10

Elizabeth Mattijsen