On Mon, Apr 19, 2004 at 11:00:33AM -0500, Abhijit A. Mahabal wrote:
: If we have a method that returns Dog if it returns anything at all, can we
: say:
:
: method foo returns Dog|undef {...}
Yes, but... You'd say that only if you wanted to allow a return
type that can be simultaneously Dog and undef, as well as either.
Remember that | is inclusive-OR, not exclusive. The default meaning of
method foo returns Dog {...}
is actually
method foo returns Dog^undef {...}
since any Object is implicitly allowed to be undef instead.
: In a similar vein, if the function reurns a dog or a refernce to an array
: , can we use Dog|Array?
Certainly. Again, you might wish to be more specific with Dog^Array,
though Dog|Array will certainly work, and is arguably more readable.
: And is this legal:
:
: given ($obj){
: when Dog: ...
: when Array: ...
: #obviously $obj can be a ref to an array, not itself an array
: }
Yes. Note that there's little distinction in Perl 6 between a ref to an
an array and the array itself. If you use an array in scalar context, you
automatically get the reference.
Larry