Great. So the flip side is, what do I return from a function so that it gives a single value if called simply, but provides optional named returns that are there if you catch them? As a capture with one positional and one named argument?

And how do you declare =that= return type ("of" type) to be strongly typed?


Larry Wall |Perl 6| wrote:
On Fri, Apr 18, 2008 at 05:34:25AM -0500, John M. Dlugosz wrote:
If a function returns different things if called in list context or item context, how do you define the "of" type (outer return type) to make the function strongly typed?

You can't.  An "of" type forces the function into a single context
(unless we allow polymorphic return types as TSa mentions).

In general, we're trying to get away from want-based context dependency
and instead attempting to encourage lazy semantic constructs such
as Captures that can behave with a wide dynamic range when actually
bound later.

For instance, Arrays now behave such that, if you use one in numeric
context, you get the number of elements, so unlike in Perl 5, you need
not decide at return time whether the array is in item or list context.
You just return the array, and it will act much like it does in Perl 5
if you use it in a numeric or boolean context.  Since the default
return type is Capture, it doesn't matter if you declare the "of"
type as Array or not, since the Capture doesn't force either item or
list context on the Array either.  It should behave much the same in
either case, except that if you declare the "of" type as Array, you
give the compiler/optimizer/type inferencer more information to work
with at compile time.


Reply via email to