Home > Raku > The absence of a riddle

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()) {};


# OUTPUT: Impossible coercion from 'Nil' into 'NotNil': method NotNil returned a type object Nil

The culprit here is .new.

dd NotNil.new;

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
  1. No comments yet.
  1. May 3, 2021 at 16:16

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: