-
-
Save lucs/387b10ac9576bf9a04fcdfdb12ce91d0 to your computer and use it in GitHub Desktop.
Raku constructor question
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
# Close, in the sense that class B has no idea of the implementation | |
# of some-A-method, but B.new returns the wrong type of instance. | |
class A { | |
has $.a; | |
method new ($x) { | |
return self.bless: :a($x / 2); | |
} | |
method some-A-method { | |
return $!a; | |
} | |
} | |
class B is A { | |
has $.b = 66; | |
method new ($x) { | |
A.new($x); | |
} | |
} | |
my $b = B.new(42); | |
# Expecting 21. | |
say $b.some-A-method; | |
# Expecting 66. | |
say $b.b; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your response. Turns out I had not explained precisely enough what I was after (it even became clear to me only as we went back and forth discussing it on IRC (raku channel on libera). The following solution (fixed up a bit by me), the thing I was after (class B knowing nothing of class A's implementation), was proposed by gfldex: