Most operators in Raku are subroutines.
1 + 2
infix:<+>( 1, 2 )
-1
prefix:<->( 1 )
You can add your own operators by creating such a subroutine.
sub postfix:<!> ( UInt \n ) { [×] 2..n }
say 5!; # 120
Because it is so easy to add operators. Operators only do one thing.
1 + 2
That operator is for doing numeric addition.
If the two things are not numbers they get turned into numbers
['a'] + ['b', 'c'] # exactly the same as 1 + 2
The +^ operator is about Integers. So if you give it something that is not
an integer it becomes one.
1.2 +^ 1; # exactly the same as 1 +^ 1
---
Note that Int:D does NOT do any coercions.
Int:D() does do coercions.
Specifically Int:D() is short for Int:D(Any). Which means it coerces from
Any to Int, and the result must be defined.
On Sat, Jan 18, 2020 at 2:39 PM ToddAndMargo via perl6-users <
[email protected]> wrote:
> Hi All,
>
> Okay, I clearly do not understand what is
> going on with these definitions, so please
> correct my assumptions!
>
>
> https://docs.raku.org/language/operators#infix_+
> https://docs.raku.org/routine/+$CIRCUMFLEX_ACCENT
>
> Question: would some kind soul please tell me how:
>
> multi sub infix:<+>($a, $b --> Numeric:D)
> multi sub infix:<+^>($a, $b --> Int:D)
>
> gets turned into
>
> $c = $a + $b, and
> $c = $a +^ $b
>
>
> This is what I understand so far about
> the definition lines:
>
> 1) "multi" means there a several way to do things
>
> 2) "sub" means subroutine: $c = +($a, $b)
>
> my $a=2; my $b=3; my $c = +($a, $b)
> 2
>
> And a participation trophy for the wrong answer.
>
> my $a=2; my $b=3; my $c = +^($a, $b)
> -3
>
> And I just won two a participation trophies!
>
>
> 3) <> is a form of literal quote
>
> 4) infix:<+> means you can call it as a sub that
> gives you back the wrong answer.
>
> $c = +($a, $b)
> $c = +^($a, $b)
>
> 5) --> Numeric:D means it return a Numeric and
> --> Int:D means it "coerces" your stuff and
> return an Int, like it or not.
>
> Yours in confusion,
> -T
>