Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 708
user6189164 Avatar asked Mar 24 '18 23:03

user6189164


People also ask

How do I create a new operator?

Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Can we create new operator?

No, unfortunately you cannot define new operators—you can only overload existing operators (with a few important exceptions, such as operator. ).

What is new operator with example?

An Example of allocating array elements using “new” operator is given below: int* myarray = NULL; myarray = new int[10]; Here, new operator allocates 10 continuous elements of type integer to the pointer variable myarray and returns the pointer to the first element of myarray.

What is new operator C++?

When new is used to allocate memory for a C++ class object, the object's constructor is called after the memory is allocated. Use the delete operator to deallocate the memory allocated by the new operator.


1 Answers

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.

like image 53
Jonathan Worthington Avatar answered Sep 30 '22 12:09

Jonathan Worthington