Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return Types: what are the enforcement details?

9 views
Skip to first unread message

Mark Stosberg

unread,
Aug 29, 2006, 8:49:38 PM8/29/06
to perl6-l...@perl.org
I'm interested in helping to write some tests for "return types", but
I'd like some clarifications about them first. Are they just
"declarations" that help Perl optimize stuff, or they actually contracts?

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;

Yuval Kogman

unread,
Aug 30, 2006, 3:20:21 AM8/30/06
to Mark Stosberg, perl6-l...@perl.org
On Tue, Aug 29, 2006 at 19:49:38 -0500, Mark Stosberg wrote:
> I'm interested in helping to write some tests for "return types", but
> I'd like some clarifications about them first. Are they just
> "declarations" that help Perl optimize stuff, or they actually contracts?

'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

0 new messages