Larry Wall wrote:
> Jonathan Worthington wrote:
> > role Bar { }
> > my Bar $x; # $x is ???
> > say $x; # ???
> > say $x.WHAT; # ???
> > # This means we can only assign to $x something that does Bar?
>
> Correct, and for the same reason. The container checks the role--it
> has little to do with what's in $x currently, which *cannot* have
> the type of Bar, since you can't instantiate that.
>
>
> > subset EvenInt of Int where { $_ % 2 == 0 };
> > my EvenInt $x; # $x is ???
> > say $x; # ???
> > say $x.WHAT; # Failure?
> > # This means we can only assign to $x something that matches the constraint
>
> Yes, again, checked by the container, not by $x. $x cannot have the
> type of a subset either! The actual type of $x is Int. Objects may
> only be blessed as valid storage types of some kind or other. Subsets
> are merely extra constraints on a storage type (here Int).
>
>
> > class Dog { }
> > class Cat { }
> > my Dog|Cat $x; # $x is ???
> > say $x; # ???
> > say $x.WHAT; # Failure?
> > # This means we can only assign to $x something that isa Dog or isa Cat
>
> Well, maybe $x is just Object, or whatever is the closest type that
> encompasses both Dog and Cat. But note that Object is just the most
> generic kind of undef, so this is more or less equivalent to doing
> no initialization in p5-think. I don't think I'm interested in making
> the run-time system calculate a Dog|Cat storage type, so it comes out
> to just a constraint.
>
> Really, the main reason we initialize with an undef of the correct sort
> is so that you can say
>
> my Dog $x .= new();
>
> But what would (Dog|Cat).new() do? Constructors are not required to
> know about subset types or roles. Constructors just create plain ordinary
> classes, I expect. Composition and constraint checking happen
> elsewhere.
Two questions:
1. Apparently, my presumption that $x.WHAT was for retrieving the
value type was wrong; from the above, it's sounding like it is
supposed to retrieve the implementation type. Is this correct? If
so, for what purpose does $x.WHAT exist that you can't do just as well
with $x itself? If it's for the stringification of the container's
name, couldn't that be accomplished just as easily by means of a
$x.HOW method, such as $x.^name?
2. How _do_ you retrieve the value type of $x? That is, how can the
program look at $x and ask for the constraints that are placed on it?
I don't see $x.HOW being useful for this, since HOW is essentially
there to let you look at the inner workings of the container; and none
of the other introspection metamethods in S12 come close to addressing
this question.
--
Jonathan "Dataweaver" Lang