Being a long time Ruby user and Lisp dabbler, Rakudo Perl 6 meta-programming capabilities ranked high on my list of curiosities.
Meta-programming can be described as "writing code that writes code". This includes code that can alter itself.
I was experimenting and discovered I could not add an attribute to a class at runtime:
class Foo does Metamodel::AttributeContainer {
submethod BUILD(:$attr) {
my $a = Attribute.new(:name($attr), :type(Str), :package(Foo));
self.add_attribute(self, $a);
}
}
my Foo $foo = Foo.new(:attr('bar'));
$foo.bar = 'baz'; # No such method 'bar' for invocant of type 'Foo'
say $foo.bar;
I found the attribute could be managed in a Hash and if I used return-rw inside FALLBACK I could even return a mutable l-value:
class Foo {
has %.attrs;
method FALLBACK(Str:D $name, *@rest) {
return-rw %!attrs{$name};
}
}
my $foo = Foo.new;
$foo.bar = 'baz';
say $foo.bar;
Thanks to Jonathan Worthington for the help:
https://stackoverflow.com/questions/57993203/how-to-add-a-class-attribute-dynamically
Page Updates |
---|
RetroPie Arcade |
Customize New Linux Install |
Cisco CCNA Study :: Home Lab Notes |
PostgreSQL Backup Script |
Generate new Factorio map |
Recent Articles |
---|
Debug a Linux Kernel in QEMU |
Install xfce4 on Debian |
Console Blackjack in Perl |
Selenium::WebDriver::Error::UnknownError |
Game of Life in C++ using the SDL2 |
Copyright © 2005 - 2022
GregDonald.com · Contact · Nautical War · CRM12
All Rights Reserved