Class:...stract(3pm)

πŸ“¦
version 0.08 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Class::Virtually::Abstract πŸ’–

okay bestie, so this is a page from the User Contributed Perl Documentation β€” it's Class:...stract(3pm) (yes the name is literally truncated like that, the header just do be like that sometimes 🫠) and it is so much better than its name suggests.

Short version: Class::Virtually::Abstract is compile-time enforcement for Class::Virtual. That's the whole pitch. That's the vibe. ✨


🌸 NAME 🌸

Class::Virtually::Abstract β€” compile-time enforcement of Class::Virtual πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

like, here's the whole situation:

package My::Virtual::Idaho;
use base qw(Class::Virtually::Abstract);

__PACKAGE__->virtual_methods(qw(new foo bar this that));

package My::Private::Idaho;
use base qw(My::Virtual::Idaho);

sub new { ... }
sub foo { ... }
sub bar { ... }
sub this { ... }
# oops, forgot to implement that()!!  Whatever will happen?!

# Meanwhile, in another piece of code!
# KA-BLAM!  My::Private::Idaho fails to compile because it didn't
# fully implement My::Virtual::Idaho.
use My::Private::Idaho;

not the # oops, forgot to implement that()!! Whatever will happen?! followed by KA-BLAM! 😭 the original author really said "let me narrate my own plot twist" and honestly? iconic. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so basically β€” this is a subclass of Class::Virtual, and what it adds is compile-time enforcement. that means: if your class is a subclass of your virtual class, you are required to implement all the virtual methods. don't do it? it will not compile. periodt. πŸ’€βœ¨

no runtime surprises. no "oh I guess the method just doesn't exist" at 3am. the compiler said nope and it meant it.


πŸ’€ BUGS and CAVEATS πŸ’€

ngl bestie, this part is the "read the whole thing" part. 🫢

  • because this whole thing relies on import(), it is important that your classes are used instead of required. that's a problem, and the author is trying to figure out a way around it. πŸ™ˆ
  • also, if a subclass defines its own import() routine (and yeah, the author has done it πŸ‘€) then Class::Virtually::Abstract's compile-time checking gets defeated. just gone. bye.
  • the author's own words: got to think of a better way to do this besides import().
so real talk: this is a known-weak spot, not a hidden feature. don't build your whole house on it and then act surprised, iykyk. 🏚️

πŸ’œ AUTHOR πŸ’œ


🌈 SEE ALSO 🌈

  • Class::Virtual β€” the parent, the blueprint, the main character this whole page is vibing off of πŸŽ€

and that's the whole page bestie!! πŸ¦‹ this beauty shipped as part of the User Contributed Perl Documentation, marked perl v5.34.0, dated 2022-06-12. go implement your virtual methods. all of them. no skipping. mwah πŸ’…βœ¨

πŸ“¦ where this page comes from