Search code examples
new-operatorrakuprefix-operator

Creating New Operator


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?


Solution

  • 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.