On Tue, May 06, 2008 at 10:54:51AM +0200, Jonathan Worthington wrote:
> Hi,
>
> I'm looking for answers/clarification on what (if taken as individual 
> programs) $x is in each case.
>
> my Int $x;         # $x is Int protoobject
> say $x;              # Int
> say $x.WHAT; # Int

What they said.  $x is initialized with an Int protoobject.

> class Foo { }
> my Foo $x;   # $x is Foo protoobject
> say $x;          # Foo
> say $x.WHAT; # Foo
> # This means we can only assign to $x something that isa Foo?

Yes, but that's because the container is checking, not the Foo
protoobject.

> 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
elsehwere.

Larry

Reply via email to