Re: Anonymous multi-subs

2015-06-25 Thread yary
On Wed, Jun 24, 2015 at 7:17 PM, Brent Laabs  wrote:
I'll just note that you can fake anon multi subs with lexical subs like
this:

my $sub = do {
proto foo (|) { * }
multi foo (Int $x) { $x + 1 }
multi foo (Str $y) { $y ~ 'a' }

&foo;
}

say $sub("hello");

I like that, and I suspect it could be made generic (a routine that takes a
list of subs, returning a multi-sub) without too much difficulty.

The sub there is still named "foo" as attested by $sub.name, but isn't
> available under that name outside of the do block.
>

I've found named lambdas very useful when debugging (in Javascript). Having
$sub.name available is a feature as far as I'm concerned!


On Wed, Jun 24, 2015 at 5:27 PM, Jon Lang  wrote:

 Or, in the body, be able to examine the actual signature passed in and
> decide what to do based on that,  That [i]could[/i] be done using a
> given/when structure, which would be equivalent to a list of signature/body
> pairs.
>
>
That works, but it means re-doing what the dispatcher already does. Nicer
to hook into existing machinery.

Shortly after writing my last message I realized that a "*signature*, *body
*pair" is exactly a sub, and that what surprises me is that there isn't
already a constructor that returns a multi-sub, given a list of subs.

S06 says "proto is a generic wrapper around the dispatch to the multis" and
"A proto is a generic dispatcher, which any given scope with a unique
candidate list will instantiate into a dispatch routine" ... that's the
machinery to hook into. I'm thinking of them as roles or types, when really
they're attributes of Routine.


Re: Anonymous multi-subs

2015-06-24 Thread Brent Laabs
I'll just note that you can fake anon multi subs with lexical subs like
this:

my $sub = do {
proto foo (|) { * }
multi foo (Int $x) { $x + 1 }
multi foo (Str $y) { $y ~ 'a' }

&foo;
}

say $sub("hello");

The sub there is still named "foo" as attested by $sub.name, but isn't
available under that name outside of the do block.


On Wed, Jun 24, 2015 at 2:27 PM, Jon Lang  wrote:

>
>
> On Wednesday, June 24, 2015, yary  wrote:
>
>> Now that I've thought about it for 90 seconds (not fully-formed idea), if
>> one were to have an anonymous multi-sub, it ought to be constructed from a
>> list of *signature*, *body *pairs.
>>
>> And/or, any non-finalized sub could have a method to add another *signature,
>> body* to its dispatch list.
>>
>> apologies if this discussion is already captured in a design doc, I am
>> posting this without having read much of the past.
>>
>
>  Or, in the body, be able to examine the actual signature passed in and
> decide what to do based on that,  That [i]could[/i] be done using a
> given/when structure, which would be equivalent to a list of signature/body
> pairs.
>
>
> --
> Jonathan "Dataweaver" Lang
>


Re: Anonymous multi-subs

2015-06-24 Thread Jon Lang
On Wednesday, June 24, 2015, yary  wrote:

> Now that I've thought about it for 90 seconds (not fully-formed idea), if
> one were to have an anonymous multi-sub, it ought to be constructed from a
> list of *signature*, *body *pairs.
>
> And/or, any non-finalized sub could have a method to add another *signature,
> body* to its dispatch list.
>
> apologies if this discussion is already captured in a design doc, I am
> posting this without having read much of the past.
>

 Or, in the body, be able to examine the actual signature passed in and
decide what to do based on that,  That [i]could[/i] be done using a
given/when structure, which would be equivalent to a list of signature/body
pairs.


-- 
Jonathan "Dataweaver" Lang