Search code examples
rakurakudo

Does anyone know why the TWEAK routine gets hit before the BUILD routine?


Minimal code:

#!/usr/bin/raku

class Widget {
    submethod TWEAK(:$content, :$styles) {
        say "t1\n";
    }
}

class File is Widget {
    submethod BUILD() {
        say "b1";
    }
}

my $xml = File.new();

And the output:

 t1

 b1

To quote from the relevant doc:

After the BUILD methods have been called, methods named TWEAK are called, if they exist, again with all the named arguments that were passed to new.

So why is TWEAK being hit before BUILD?

I'm using the version of Rakudo that comes with Fedora 32 (rakudo-0.2020.02-1.fc32.x86_64).


Solution

  • A quick experiment adding a TWEAK and BUILD method to each example class where they are missing shows the order of calls is Widget::BUILD -> Widget::TWEAK -> File::BUILD -> File::TWEAK.

    So I think the problem is the wording of the documentation "After the BUILD methods have been called, methods named TWEAK are called, if they exist, again with all the named arguments that were passed to new."

    I think this should possibly be "After the BUILD methods have been called for each class, methods named TWEAK are called for that class, if they exist, again with all the named arguments that were passed to new."

    This would then document what seems to be happening.

    Note : since answering this question the documentation has been updated to reflect how the code is being run.