The absence of a riddle
Raku riddles have become popular. By chance, I came across a riddle generated by Rakudo — an emergent riddle so to speak.
class NotNil is Nil {};
class C {
method NotNil() { NotNil.new }
};
sub s(NotNil()) {};
s(NotNil.new)
# OUTPUT: Impossible coercion from 'Nil' into 'NotNil': method NotNil returned a type object Nil
The culprit here is .new
.
dd NotNil.new;
# OUTPUT: Nil
The constructor Nil.new
will always return Nil
. As lizmat pointed out, we need to provide our own constructor.
class NotNil is Nil {
method new { self.CREATE }
}
The lesson I learned from this is that I can’t rely on inheritance for object creation. This is something we need to keep in mind when subclassing from a 3rd party module.
Categories: Raku
Comments (0)
Trackbacks (1)
Leave a comment
Trackback
-
May 3, 2021 at 16:162021.18 Double Comma – Rakudo Weekly News