Skip to content

Instantly share code, notes, and snippets.

@wayland
Last active September 28, 2024 09:51
Show Gist options
  • Save wayland/7899d75548c470c17a1aee63961e59fc to your computer and use it in GitHub Desktop.
Save wayland/7899d75548c470c17a1aee63961e59fc to your computer and use it in GitHub Desktop.
Construction problem example
#!/usr/bin/raku
# Adds BUILD-ARGS and POST-TWEAK
class BetterNew {
method new(*@positional is copy, *%named is copy) {
say "BetterNew.new";
dd @positional;
for self.^mro -> $type {
say "Looking for BUILD-ARGS on { $type.^name }";
if $type.can('BUILD-ARGS') {
my @rvs = $type.BUILD-ARGS(@positional, %named);
@positional := @rvs[0];
%named := @rvs[1];
}
}
dd %named;
my $this = callwith(self, |@positional, |%named);
# This is the point where I expect the object construction to have completed...
say "bn1";
for reverse self.^mro -> $type {
say "Looking for POST-TWEAK on { $type.^name }";
if $type.can('POST-TWEAK') {
$type.POST-TWEAK();
}
}
return $this;
}
}
class A is BetterNew {
has Str $.object-name is rw is built;
has Str $.object-slug is rw;
aubmethod POST-TWEAK() {
# ... so why does this not work?
$!object-slug = $!object-name;
# $!object-slug ~~ s:g/\s/_/;
}
}
class B is A {
submethod BUILD-ARGS(@positional, %named) {
say "B.BUILD-ARGS";
%named<object-name> = 'app ape';
return @positional, %named;
}
submethod POST-TWEAK() {
say "B.POST-TWEAK";
say "$.object-slug";
}
}
my $b = B.new();
say $b.object-slug;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment