develooper Front page | perl.perl6.language | Postings from July 2001

How easy will it be to install new methods into builtins?

Brent Dax
July 26, 2001 22:48
How easy will it be to install new methods into builtins?
Message ID:
I was thinking about it today, and it occurred to me that we could make it very easy to install new
methods into the builtin types (SCALAR, ARRAY, HASH, CODE).  What if we simply had a package for
each of these builtin types such that you could say something like:

	sub ARRAY::merge(ARRAY $lhs, ARRAY $rhs) {
		my @result;

		my $ubound=($lhs.length < $rhs.length ? $rhs.length : $lhs.length) - 1;	#here i'm calling
ARRAY::length, not SCALAR::length

		for(0..$ubound) {
			push @result, $lhs[$_ % $lhs.length];
			push @result, $rhs[$_ % $rhs.length];

		return @result;

and expect that to be callable like C<@a1.merge(@a2)>?

Here's another possibility along the same lines:

	package SparseArray;	#yes, I know package is going away...
	@ISA=('ARRAY');		#without this, the stash assignment later would be illegal

	#do other stuff here...

and then later be able to say something like C<%main::{'@sparsearray'}=new SparseArray> and expect
*that* to work right?

This could be a more general tying mechanism.  It could also probably be integrated into vtables,
although I don't really know enough about how they work to say that for sure.  (Obviously, this is
more of a Perl-level interface idea than an internals idea.  I suppose that's why I'm sending it
to -language.  :^) )

BTW, one other random thought I came up with while I was writing this:  Could inheritance be a

	class My::Class is a('Other::Class');	#My::Class::ISA=('Other::Class')

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