As this little script shows, both "inner" and "of" are valid syntax now
with pugs, but neither is considered an error to throw an error when the
different type is actually returned.
I'd like see it documented in exactly what cases errors should be thrown
here.
For reference, return are described here:
http://feather.perl6.nl/syn/S06.html#Return_types
demo:
sub foo of Array {
my %h = ( a => 1 );
return %h;
}
sub zoo returns Array {
my %h = ( a => 1 );
return %h;
}
# Hashes are happily returned, despite the Array return types.
my %b = foo(); say %b.perl;
my %c = foo(); say %c.perl;
'of' is the contractual form, 'returns' is a constraint but it's
more like a cast.
> demo:
>
> sub foo of Array {
> my %h = ( a => 1 );
> return %h;
> }
> sub zoo returns Array {
> my %h = ( a => 1 );
> return %h;
> }
>
> # Hashes are happily returned, despite the Array return types.
> my %b = foo(); say %b.perl;
> my %c = foo(); say %c.perl;
^-- z ?
Intuitively I would say that both subroutines force the hash into an
array, at minimum, and foo might be checked more thoroughly.
In the case of foo(), foo itself might not compile, or my %b = foo()
might not compile, or both.
In the case of zoo(), i think it's just a runtime conversion to an
array. There's no reason why this conversion can't happen explicitly
as well as implicitly, like with my %h = () = %other_hash.
However, conversions that cannot be made could be cought at compile
time, emitting a warning on an error depending if the runtime is a
warning or an error.
--
Yuval Kogman <nothi...@woobling.org>
http://nothingmuch.woobling.org 0xEBD27418