Dynamic declaration
Shortly after my last blog post, Stashes raised a question. Coincidence? Conspiracy? You decide! Anyway, the EVAL
caught my eye, because with it we can dynamically create compile time constructs such as a package
.
our package EXPORTHOW {
}
sub EXPORT($declarator-name = 'registered') {
use MONKEY-SEE-NO-EVAL;
OUR::EXPORTHOW::DECLARE := EVAL q:s:to /EOH/;
package DECLARE {
constant $declarator-name = MetamodelX::RegisteredHOW;
}
EOH
Map.new
}
Thanks to be our
-scoped, the package EXPORTHOW
can be modified at runtime. The EVAL
allows to define a constant
at runtime. Runtime in this context, is when the use
statement of a consuming module is executed.
use Registered 'remembered', :recall-types;
remembered Foo {
method answer { 42 }
method common { self.^name }
}
With this technique, we allow the user of a module to decide what symbols are being used for a declarator. Pretty handy, if a module is added late to a project, which might have occupied a given symbol already. Quite some time ago, I lamented a little lizmats decision to modify class
with InterceptAllMethods
. This is now a solvable problem.
I once believed Raku to be less dynamic then Perl. Looks like I have to reconsider.
-
August 30, 2021 at 15:262021.35 Superseding – Rakudo Weekly News