Is Dog|undef a legal type?

2004-04-19 Thread Abhijit A. Mahabal
If we have a method that returns Dog if it returns anything at all, can we
say:

method foo returns Dog|undef {...}

In a similar vein, if the function reurns a dog or a refernce to an array
, can we use  Dog|Array?

And is this legal:

given ($obj){
when Dog: ...
when Array: ...
   #obviously $obj can be a ref to an array, not itself an array
}

--Abhijit

Abhijit A. Mahabal  http://www.cs.indiana.edu/~amahabal/



Re: Is Dog|undef a legal type?

2004-04-19 Thread Larry Wall
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


Re: Is Dog|undef a legal type?

2004-04-19 Thread Juerd
Abhijit A. Mahabal skribis 2004-04-19 11:00 (-0500):
   when Dog: ...
   when Array: ...

Shouldn't that be:

when Dog { ... }
when Array { ... }

Or is there some .when that I have not yet heard of?


Juerd


Re: Is Dog|undef a legal type?

2004-04-19 Thread Abhijit A. Mahabal

 Abhijit A. Mahabal skribis 2004-04-19 11:00 (-0500):
  when Dog: ...
  when Array: ...

 Shouldn't that be:

 when Dog { ... }
 when Array { ... }

 Or is there some .when that I have not yet heard of?

Guilty as charged. My Perl6 is getting rusty...

--Abhijit


Re: Is Dog|undef a legal type?

2004-04-19 Thread Larry Wall
On Mon, Apr 19, 2004 at 07:01:34PM +0200, Juerd wrote:
: Abhijit A. Mahabal skribis 2004-04-19 11:00 (-0500):
:  when Dog: ...
:  when Array: ...
: 
: Shouldn't that be:
: 
: when Dog { ... }
: when Array { ... }

Yes, that's how it should be written.

: Or is there some .when that I have not yet heard of?

Nope.  I was just reading the previous as pseudocode, so I didn't say
anything about it.

Larry