Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to safely redeclare a symbol?

Tags:

raku

I often find myself experimenting in the REPL and I will say something like:

subset Bar of Int where * %% 57;

Then I play around with checks on the Bar-ness for things for a bit.

Everything is happy, until I realize that I want to change the definition of Bar.

If I just redefine Bar, I get a Redeclaration of symbol exception.

I tried using MONKEY-TYPING and augment like this:

use MONKEY-TYPING;
augment subset Bar of Int where * %% 37;

But that netted me the same error.

Why do I want this? So I can iterate on my subset (or class, or other symbol) definitions, while reusing the tests I've already typed that are in my history.

like image 561
daotoad Avatar asked Feb 04 '23 16:02

daotoad


2 Answers

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! :-)

like image 196
Elizabeth Mattijsen Avatar answered Feb 06 '23 07:02

Elizabeth Mattijsen


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.

like image 27
cowbaymoo Avatar answered Feb 06 '23 05:02

cowbaymoo