Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem defining postfix operators in raku REPL

Tags:

raku

rakudo

If I have the following program in raku it works well:

trabajando-en-piensa-en-raku on ī‚  master [?] via šŸ’Ž v2.6.5
āÆ cat factorial.raku

sub factorial( $n ) {
   [*] 1 .. $n;
}

sub postfix:<!>( $n ) {
    [*] 1 .. $n;
}

my $n = 5;

say "El factorial de $n es {factorial $n}";

say "Si calculamos $n! obtenemos {$n!}";


trabajando-en-piensa-en-raku on ī‚  master [?] via šŸ’Ž v2.6.5
āÆ raku factorial.raku
El factorial de 5 es 120
si calculamos 5! obtenemos 120

But if I define this functions in the raku REPL I get:

> * * &factorial
> 5
> El factorial de 5 es 120

It works normally and as expected for the factorial function, but I get this for the ! operator:

> * * &postfix:<!>
> ===SORRY!=== Error while compiling:
Negation metaoperator not followed by valid infix
------> say "Si calculamos $n! obtenemos {$n!ā}";
    expecting any of:
        infix
        infix stopper

I need an special sintax in order to define operators in raku REPL, or is depending how the environment is loaded.

I'm using In OSX Catalina

trabajando-en-piensa-en-raku on ī‚  master [?] via šŸ’Ž v2.6.5
āÆ rakubrew versions
  system
  moar-2020.07
* moar-2020.08.2
like image 738
anquegi Avatar asked Oct 22 '20 07:10

anquegi


1 Answers

I'm afraid the REPL in its current state, has several deficiencies regarding several Raku features, such as native variables and operator definition, IF these are executed in more than one line.

Currently the REPL is basically executing an EVAL statement for each line, with not enough information shared between invocations. This will not change in the short run. It might get better when the rakuast branch lands, sometime next year.

like image 154
Elizabeth Mattijsen Avatar answered Oct 29 '22 13:10

Elizabeth Mattijsen