Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku operator overloading

Using the following code:

use v6d;

# sub circumfix:<α ω>( @a ) {
#     @a[0] >= @a[1] & @a[0] <= @a[2];
# };

sub circumfix:<α ω>( $a, $b, $c ) {
    $a >= $b & $a <= $c;
};

if (α <5 0 10> ω) {
    say 'Truthy';
}
else {
    say 'Falsey';
}

Results in:

(base) hsmyers@BigIron:~/board$ perl6 ./op.p6
Too few positionals passed; expected 3 arguments but got 1
  in sub circumfix:<α ω> at ./op.p6 line 7
  in block <unit> at ./op.p6 line 11

Whereas switching the commented block for the other definition results in:

(base) hsmyers@BigIron:~/board$ perl6 ./op.p6
Truthy

The broken version (with three parameters) is the one I want, could someone explain why it is broken?

like image 897
hsmyers Avatar asked Dec 08 '19 19:12

hsmyers


2 Answers

<5 0 10> literally constructs a List, a single List.

An analogy would be a list of things to do, a todo list. How many things is a todo list? It's 1 -- one todo list.

Thus you get the error message:

expected 3 arguments but got 1

What you want is to specify that you want one value that is itself made up of several values. Here's one way to do that:

sub circumfix:<α ω>( ( $a, $b, $c ) ) ...

The additional surrounding ( and ) cause destructuring.

like image 188
raiph Avatar answered Dec 09 '22 12:12

raiph


D:\>6e "say <5 0 10>"
(5 0 10)

These aren't three arguments. It's a list of three values (of type IntStr) and therefore a single argument.

like image 22
Holli Avatar answered Dec 09 '22 11:12

Holli