develooper Front page | perl.perl5.porters | Postings from May 2003

Re: Can I OVERLOAD the assignment operator? Please???

Thread Previous | Thread Next
From:
John Peacock
Date:
May 23, 2003 04:12
Subject:
Re: Can I OVERLOAD the assignment operator? Please???
Message ID:
3ECE01C2.1030305@rowman.com
Nick Ing-Simmons wrote:
> I mean the technical how-it-will-work, and how-it-won't break existing 
> modules with overload etc. discussions ;-)
> From last time round those will take a while.
> 

My goal is to make it transparent to existing overload modules, no significant 
performance hit for other code, and as simple as possible to program.  It should 
also make your teeth whiter and your breath smell minty fresh. YMMV ;~)

As a first pass, I want to investigate whether I can set the SMG flag on the 
overloaded RV and extend the current mg_set code to handle it.  The specific 
callback will be stored with the other package callbacks in the class stash, so 
it will differ from U-magic in that all objects of that type must have the same 
callback.

What I would like to do would be to extend "overload '=' => &callback" so that I 
don't need to create a new keyword, but I don't yet know whether that would be 
possible.  The issue, of course, will be clashing metaphors if both the LHS and 
RHS are themselves magical:

	$a = new Class::A "1.0"; #implements assignment overload
	$b = new Class::B "fish"; #also overloaded objects
	...sometime later...
	$a = $b; #here there be dragons

The simplest methodology would be to effectively rewrite that last line as

	$a = "$b"; # safe???

This won't affect the behavior of Class:B (which can predate the change) except 
that the "$a = $b" will no longer be a shallow copy.  This would mean that 
Class::A would be sticky, but that _is_ the point after all.  I suppose we could 
also allow Class::A to choose whether to use $b->stringify or $b->numify as its 
input.  Hmmm...

Basically, I think the logic has to be this:

	$a = "these aren't the droids you are looking for";
	$b = new Something::Wicked 1.3;
	...civilizations rise and fall...
	$a = $b; # shallow copy, old string abandoned
	...techtonic plates shift...
	++$a; # which is implemented as
	# $a = $a->clone(undef,"");
	# $a->incr(undef,"");
	...the sun expands in supernova...
	$a = 42; # called as $a->assign(42)

I presume that the current code knows to do the clone() because it can see that 
more than one object is referring to the same value.  Presumably, the same logic 
can be used to know when to clone() prior to assign().

John

-- 
John Peacock
Director of Information Research and Technology
Rowman & Littlefield Publishing Group
4720 Boston Way
Lanham, MD 20706
301-459-3366 x.5010
fax 301-429-5747


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About