develooper Front page | perl.perl6.users | Postings from June 2020

Re: proto and multi

Thread Previous | Thread Next
From:
Fernando Santagata
Date:
June 29, 2020 18:32
Subject:
Re: proto and multi
Message ID:
CAJ+jyGhDsdjS6BTmm9XdXUx3PZXbvk_4Mz9_OGnzh2c=NtPZ6w@mail.gmail.com
On Mon, Jun 29, 2020 at 8:22 PM Richard Hainsworth <rnhainsworth@gmail.com>
wrote:

> a) I don't understand why the white space matters, but clearly it does. So
> the token is '{*}' and not braces around a Whatever-star.
>
For an explanation see the thread "Playing with protos and phasers" that I
started here three days ago.

b) Removing the space yields the following response.
> in string
> Cannot resolve caller handle(NewClass:D: List:D); none of these signatures
> match:
>     (NewClass: Str $s, *%_)
>     (NewClass: Positional @s, *%_)
>   in method handle at test.raku line 15
>   in block <unit> at test.raku line 30
>
> Not sure why the List:D is not being matched to Positional. Is the List:D
> refering to the |c signature capture ??
>
> Since 'in string' was printed, one of the methods was reached. Confused.
>
> c) Writing out all the code in each method is what I already have. But I'm
> looking for ways to factor out common code.
>
> Regards
> On 29/06/2020 18:44, Fernando Santagata wrote:
>
> After deleting the spaces as suggested, there's a "Positional" too many.
> I guess you can rewrite that method declaration as
>
> multi method handle(@s)
>
> or
>
> multi method handle(Positional $s)
>
> and adjust the method's body.
>
> On Mon, Jun 29, 2020 at 7:37 PM yary <not.com@gmail.com> wrote:
>
>> It looks like you have spaces in the token { * } can you try it without,
>> using this {*} instead?
>>
>> On Mon, Jun 29, 2020, 1:29 PM Richard Hainsworth <rnhainsworth@gmail.com>
>> wrote:
>>
>>> I have several multi methods.
>>>
>>> All of them have the same first statement, then differ depending on the
>>> signature.
>>>
>>> proto seems to be a way to factor out the common statement, and there is
>>> a phrase in the Documentation that * can affect the dispatch, viz:
>>>
>>> "You can give the proto a function body, and place the {*} where you
>>> want the dispatch to be done. This can be useful when you have a "hole" in
>>> your routine that gives it different behavior depending on the arguments
>>> given:"
>>>
>>> The docs give and example proto, but unfortunately, not how this works
>>> with other multi's.
>>>
>>> So  I tried this:
>>>
>>> class NewClass {
>>>     has $.debug is rw = False;
>>>     has $.value is rw = 'Initial value';
>>>
>>>     proto method handle( |c ) {
>>>         note "value is $.value" if $.debug;
>>>         { * }    }
>>>     multi method handle(Str $s) {
>>>         $.value = $s;
>>>         say 'in string'    }
>>>     multi method handle(Positional @s) {
>>>         $.value = @s[0];
>>>         say 'in positional'    }
>>> }
>>> my NewClass $x .= new;
>>> $x.handle('hello world');$x.handle(<hello world>);$x.debug = True;$x.handle('hello world');$x.handle(<hello world>);
>>>
>>> #raku test.raku
>>> #value is Initial value
>>> #value is Initial value
>>>
>>> I am wondering how to use proto and {*}
>>>
>>>
>
> --
> Fernando Santagata
>
>

-- 
Fernando Santagata

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About