Nathan Wiger <[EMAIL PROTECTED]> writes:
> It seems potentially useful to be able to say:
>
> my Dog, Cat $fluffy;
>
> As a way to say "$fluffy can be either a Dog or a Cat". Since variables
> are prefixed, anything comma-separated up to the variable is an
> alternate class for that variable:
Eurgh.
sub Pet::type { ref $_[0] }
my Pet $fluffy;
die unless $fluffy->type =~ s/^(?:Dog|Cat)$/;
By declaring a variable as being one of many types you throw away any
possible compile time optimizations that may be possible (unless you
add the restriction that said types have a common interface...)
Note that a possible syntax for doing what you want could be:
my Pet $fluffy : isa(any(qw/Dog Cat/));
sub Pet::MODIFY_SCALAR_ATTRIBUTES {
my($package,$target,@attribs) = @_;
@attribs = $package->SUPER::MODIFY_SCALAR_ATTRIBUTES($target,@attribs);
my @unhandled;
for my $attr (@attribs) {
push(@unhandled, $attr), next unless $attr =~ /^isa\(.*\)/;
use attributes Assertion => $target,
"invariant(!defined(\^_) || \^_->$attr)";
}
return @unhandled;
}
Note the ad hoc syntax for assertions. If we only had currying already
I'd be looking at turning this into a perl5 module. I might do it
anyway.
Superpositions are rather cool too...
--
Piers