[Perl5-syntax] signature syntax for method return values

Ash Berlin ash_cpan at firemirror.com
Wed Feb 25 20:55:08 GMT 2009


On 25 Feb 2009, at 20:13, Paul LeoNerd Evans wrote:

> 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
>  {
>     ...
>  }

Java does this statically at compile time. We can't really do this,  
and I don't think we ever really want to.

>
>
> 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]
>

Either one of those is possible if anon subs from method return a  
wrapper object that ISA MOP object of some sort and auto delegates - 
 >() somehow. Not sure if that is doable in practice tho.

-ash



More information about the Perl5-syntax mailing list