develooper Front page | perl.perl6.language | Postings from May 2005

Virtual methods

Thread Next
Aaron Sherman
May 18, 2005 07:09
Virtual methods
Message ID:
In Perl 6, I don't think we need to tag methods as "virtual" like C++
does, since we have the handy yadda, yadda to do that for us.

However, there is a variant of C++'s virtual that I'd love to see. By
default a role cannot override the methods of a class, but if it could
override those methods specifically marked with the virtual trait, then
we could define stub methods in classes that don't have a specific
behavior until a more concrete role is mixed in.

This gives you a form of auto-loading like delegation, but with less
storage overhead (since there's no encapsulation until you need it).
Here's an example:

	role X {
		has Str $.string handles<ucfirst>;
		# I'll write something like an accessor to avoid brining
		# up some questions around how virtual methods interact
		# with auto-accessors just yet.
		method setstring(Str $string) { $.string = $string }
	class Y {
		method setstring(Y $me: Str $string)
		    is virtual {
			$me does X;
	my Y $var;
	$var.setstring("hello, world"); # overrides setstring
	say $var.ucfirst; # says "Hello, world"
	$var.setstring("bye, now"); # calls existing setstring
	say $var.ucfirst; # says "Bye, now"

You can probably tell that I'm about to suggest that this would be the
most efficient way to implement the dynamic functionality of Any, and
given the recent ponie/parrot discussions around flags, I think using
virtual methods as flags is probably the right way to go....

Aaron Sherman <>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About