Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Perl 6 class final?

Tags:

raku

For example, I have such code:

class Foo {}
class Bar is Foo {}

While this action usefulness is arguable, is it possible to prohibit inheritance from Foo?

like image 468
Takao Avatar asked Nov 24 '18 16:11

Takao


1 Answers

There's no built-in way in the Perl 6 language to prohibit inheritance of a class, and this was a deliberately taken design decision. It's also not clear one could achieve this even with meta-programming, since the act of inheritance is driven by the subclass, with the parent class having no say in the process.

It is, however, possible to mark individual methods as not being inherited, by declaring them as a submethod instead of a method.

class P {
    submethod m() { say "not inherited" }
}
class C is P {
}

say ?P.can('m');  # True
say ?C.can('m');  # False
like image 84
Jonathan Worthington Avatar answered Nov 06 '22 13:11

Jonathan Worthington