Is there a way to safely redeclare a symbol?

The REPL has its shortcomings. It is an elaborate construction of EVAL statements that try to work together. Sometimes that doesn't work out.

I guess the best we could do, is to introduce a REPL command that would make it forget everything it has done before. Patches welcome! :-)


I think the REPL does part of its magic by EVAL-ing each new input in a new nested lexical scope. So, if you declare things with my then you can shadow them with declarations entered later:

my subset Bar of Int where * %% 57;
sub take-Bar(Bar $n) { say "$n is Bar" }
take-Bar 57;

my subset Bar of Int where * %% 42;
sub take-Bar(Bar $n) { say "$n is Bar" }
take-Bar 42;

If you omit my, then for subset and class declarations, our will be used, and since our is actually my + adding the symbol to the enclosing package...; turns out if you delete the symbol from the package, you can then shadow it again later:

subset Bar of Int where * %% 57;
GLOBAL::<Bar>:delete;
subset Bar of Int where * %% 42;
42 ~~ Bar;

NOTE: These results are just from my experiments in the REPL. I'm not sure if there are other unknown side effects.

Tags:

Raku