Creating New Operator
Asked Answered
H

1

5

I'm trying to make ¬ a logical negation operator.

¬ True;

multi sub prefix:<¬> ($n) {
        return not $n;
}

When I run the above program, it returns this error:

$ perl6 test.pl6  
===SORRY!=== Error while compiling /home/devXYZ/test.pl6 Bogus statement at /home/devXYZ/test.pl6:1
------> <BOL>⏏¬ True;
expecting any of:
    prefix
    term

Does anyone know what the cause might be?

Haystack answered 24/3, 2018 at 23:19 Comment(0)
F
7

The declaration of the new operator must appear before its usage. Changing the program to:

multi sub prefix:<¬> ($n) {
    return not $n;
}
say ¬ True;

Makes it work fine.

Perl 6 has strict one-pass parsing rules. Therefore, order matters with anything that influences the language being parsed - such as by introducing a type or a new operator.

Fagaly answered 25/3, 2018 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.