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

Re: overloading = [a solution]

From:
Damian Conway
Date:
May 5, 2000 23:06
Subject:
Re: overloading = [a solution]
Message ID:
200005060606.QAA07259@indy05.csse.monash.edu.au
   > >    > How is it different from simple tie()ing?
   > > 
   > > For most folks outside P5P tie()ing isn't "simple".
   > 
   > What makes your API simpler?

My API:

	use Class::ifiedVars;

	classify $spot  => 'Dog';
	classify $fluff => 'Cat';
	classify $rex   => 'Tyrannosaur';

	type AlleyCat => 'AlleyCat'
	AlleyCat ($felix, $tom, $snagglepuss);


Tie()ing:

	package Tie::OneTypeOnly;

	sub TIESCALAR { bless { type => $_[1] }, $_[0] }
	sub FETCH     { $_[0]->{value} }
	sub STORE     { my $newtype = ref $_[1];
			croak "Can't assign $newtype to $_[0]->{type} var"
				unless $newtype->isa($_[0]->{type});
			return $_[0]->ASSIGN($_[1])
				if ($_[0] && $_[0]->can('ASSIGN');
			$_[0]->{value} = $_[1];
		      }

	tie $spot,        'Tie::OneTypeOnly', 'Dog';
	tie $fluff,       'Tie::OneTypeOnly', 'Cat';
	tie $rex,         'Tie::OneTypeOnly', 'Tyrannosaur';
	tie $felix,       'Tie::OneTypeOnly', 'AlleyCat';
	tie $tom,         'Tie::OneTypeOnly', 'AlleyCat';
	tie $snagglepuss, 'Tie::OneTypeOnly', 'AlleyCat';


And the tie()d version *still* doesn't support per-object assignment
semantics, dispatch to ASSIGN without type-enforcement, or use by people
who don't (want to) know about tie()ing.


Damian



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