Last active
September 28, 2024 09:51
-
-
Save wayland/7899d75548c470c17a1aee63961e59fc to your computer and use it in GitHub Desktop.
Construction problem example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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