[Perl5-syntax] signature syntax for method return values

Paul LeoNerd Evans leonerd at leonerd.org.uk
Wed Feb 25 20:13:54 GMT 2009


On Wed, Feb 25, 2009 at 08:00:59PM +0000, Ash Berlin wrote:
>
> On 25 Feb 2009, at 19:34, 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 { ... }
>
> of those two I prefer the latter.

Me too. The brackets work nicer - it takes two parameters, Str and Int,
and returns HashRef. Otherwise, we might think it takes two parameters,
Str, and [a function taking Int returning HashRef].

> 'returns' has the advantage of being nice and explicit about what it  
> means, and goes well with the not making it look like line noise.
>
> method to_array (Str $arg) returns ArrayRef[Str] {
>     return split /,/, $argl
> }

That looks nice. Keeps the args and the ret very clearly visually
separated. It also allows us to do a Java, should we want [1]:

  method get_file (Str $filename)
                  returns File
                  throws IOException
  {
     ...
  }

Speaking of functionals, any idea how deeply the types of these could be
supported? Can we do a map-alike (using ArrayRef) by:

  method mutate_ints (Code(Int) returns Int $body, ArrayRef[Int] $nums)
                     returns ArrayRef[Int]
  {
     ...
  }

Or maybe move the name further up?

  method mutate_ints (Code(Int) $body returns Int, ArrayRef[Int] $nums)
                     returns ArrayRef[Int]

---

[1] Though in the specific case of checked exceptions, I suspect we don't.
    Maybe other use cases?

-- 
Paul "LeoNerd" Evans

leonerd at leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.scsys.co.uk/pipermail/perl5-syntax/attachments/20090225/8cee5bf6/attachment.pgp


More information about the Perl5-syntax mailing list