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 <[EMAIL PROTECTED]> http://nothingmuch.woobling.org 0xEBD27418
pgpZuk0rgo5oy.pgp
Description: PGP signature