[Perl5-syntax] signature syntax for method return values

Robert Sedlacek rs at 474.at
Wed Feb 25 19:53:53 GMT 2009


Florian Ragwitz wrote:
 > I wanted to add return value type constraints to
 > MooseX::Method::Signatures for a while now, but I have no idea
 > what the
 > signature syntax for that should look like.
 >
 > Any ideas?
 >
 > I was thinking something like
 >
 >   method foo (Str $a, Int $b $some_seperator HashRef) { ... }
 >
 > or
 >
 >   method foo (Str $a, Int $b) $some_seperator HashRef { ... }
 >
 > where $some_seperator might be something like '-->', '->', '→' or
 > 'Returns'.

I personally would like something like

   method foo (Str $x, Int $y) -> Bool { ... }

But I would also love to be able to say

   method foo (Str $x) -> (Int $y) -> Bool { ... }

and have it be transformed to

   method foo (Str $x) -> CodeRef {
     return fun (Int $y) -> Bool {
       ...
     };
   }

(fun being used as a replacement for a sub with signatures)

But it seems this is not-so-much intuitive for most folk, judging on 
reactions on IRC.

For a simple return type definition I would suggest

   method square returns Int (Int $x, Int $y) { ... }

(I picked this up from someone else on IRC, but I don't know who it was 
anymore).

This has the advantage that the type identifiers are close together. 
While this is also true for the (Int $x, Int $y --> Str $z) approach, it 
  feels a bit weird to me to have the return type declared inside the
parameter signature.

Like the above complex (autocurrying?) example, this opinion is probably 
based on my being influenced by Scheme. I find it reads easier if the 
parameter declaration matches the method/function application/call:

   method foo($x, $y) { ... }
   $self->foo(13, 14);

So, that's all from me for the moment :)

-Robert



More information about the Perl5-syntax mailing list