proto and multi

--------------D4E3D834C77B7F2AAF80BB6A
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

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


--------------D4E3D834C77B7F2AAF80BB6A
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>I have several multi methods.</p>
    <p>All of them have the same first statement, then differ depending
      on the signature.</p>
    <p>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:</p>
    <p>"You can give the <code>proto</code> a function body, and place
      the <code>{*}</code> 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:" <br>
    </p>
    <p>The docs give and example proto, but unfortunately, not how this
      works with other multi's.<br>
    </p>
    <p>So  I tried this:<br>
    </p>
    <pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:9.0pt;"><span style="color:#000080;font-weight:bold;">class </span><span style="color:#4b0082;">NewClass </span>{
    <span style="color:#000080;font-weight:bold;">has </span><span style="color:#800080;">$.debug </span><span style="color:#000080;font-weight:bold;">is </span><span style="color:#4b0082;">rw </span><span style="color:#8b0000;">= </span><span style="color:#4b0082;">False</span>;
    <span style="color:#000080;font-weight:bold;">has </span><span style="color:#800080;">$.value </span><span style="color:#000080;font-weight:bold;">is </span><span style="color:#4b0082;">rw </span><span style="color:#8b0000;">= </span><span style="color:#008000;font-weight:bold;">'Initial value'</span>;

    <span style="color:#000080;font-weight:bold;">proto method </span><span style="color:#ff4500;">handle</span>( <span style="color:#8b0000;">|</span><span style="color:#4b0082;">c </span>) {
        <span style="color:#ff4500;">note </span><span style="color:#008000;font-weight:bold;">"value is </span><span style="color:#800080;">$</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">value</span><span style="color:#008000;font-weight:bold;">" </span><span style="color:#000080;font-weight:bold;">if </span><span style="color:#800080;">$</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">debug</span>;
        <span style="color:#4b0082;">{ * }
</span><span style="color:#4b0082;">    </span>}
    <span style="color:#000080;font-weight:bold;">multi method </span><span style="color:#ff4500;">handle</span>(<span style="color:#4b0082;">Str </span><span style="color:#800080;">$s</span>) {
        <span style="color:#800080;">$</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">value </span><span style="color:#8b0000;">= </span><span style="color:#800080;">$s</span>;
        <span style="color:#ff4500;">say </span><span style="color:#008000;font-weight:bold;">'in string'
</span><span style="color:#008000;font-weight:bold;">    </span>}
    <span style="color:#000080;font-weight:bold;">multi method </span><span style="color:#ff4500;">handle</span>(<span style="color:#4b0082;">Positional </span><span style="color:#800080;">@s</span>) {
        <span style="color:#800080;">$</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">value </span><span style="color:#8b0000;">= </span><span style="color:#800080;">@s</span>[<span style="color:#0000ff;">0</span>];
        <span style="color:#ff4500;">say </span><span style="color:#008000;font-weight:bold;">'in positional'
</span><span style="color:#008000;font-weight:bold;">    </span>}
}

<span style="color:#000080;font-weight:bold;">my </span><span style="color:#4b0082;">NewClass </span><span style="color:#800080;">$x </span><span style="color:#8b0000;">.= </span><span style="color:#ff4500;">new</span>;

<span style="color:#800080;">$x</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">handle</span>(<span style="color:#008000;font-weight:bold;">'hello world'</span>);
<span style="color:#800080;">$x</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">handle</span>(<span style="color:#008000;font-weight:bold;">&lt;hello world&gt;</span>);
<span style="color:#800080;">$x</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">debug </span><span style="color:#8b0000;">= </span><span style="color:#4b0082;">True</span>;
<span style="color:#800080;">$x</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">handle</span>(<span style="color:#008000;font-weight:bold;">'hello world'</span>);
<span style="color:#800080;">$x</span><span style="color:#8b0000;">.</span><span style="color:#ff4500;">handle</span>(<span style="color:#008000;font-weight:bold;">&lt;hello world&gt;</span>);

#raku test.raku 
#value is Initial value
#value is Initial value

I am wondering how to use proto and {*}
</pre>
  </body>
</html>

--------------D4E3D834C77B7F2AAF80BB6A--
0
rnhainsworth
6/29/2020 5:29:41 PM
perl.perl6.users 1426 articles. 0 followers. Follow

6 Replies
11 Views

Similar Articles

[PageSpeed] 25

--000000000000d544fa05a93c81f4
Content-Type: text/plain; charset="UTF-8"

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

--000000000000d544fa05a93c81f4
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">It looks like you have spaces in the token { * } can you =
try it without, using this {*} instead?</div><br><div class=3D"gmail_quote"=
><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Jun 29, 2020, 1:29 PM Richar=
d Hainsworth &lt;<a href=3D"mailto:rnhainsworth@gmail.com">rnhainsworth@gma=
il.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 =20

   =20
 =20
  <div>
    <p>I have several multi methods.</p>
    <p>All of them have the same first statement, then differ depending
      on the signature.</p>
    <p>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:</p>
    <p>&quot;You can give the <code>proto</code> a function body, and place
      the <code>{*}</code> where you want the dispatch to be done. This
      can be useful when you have a &quot;hole&quot; in your routine that g=
ives it
      different behavior depending on the arguments given:&quot; <br>
    </p>
    <p>The docs give and example proto, but unfortunately, not how this
      works with other multi&#39;s.<br>
    </p>
    <p>So=C2=A0 I tried this:<br>
    </p>
    <pre style=3D"background-color:#ffffff;color:#000000;font-family:&#39;D=
ejaVu Sans Mono&#39;;font-size:9.0pt"><span style=3D"color:#000080;font-wei=
ght:bold">class </span><span style=3D"color:#4b0082">NewClass </span>{
    <span style=3D"color:#000080;font-weight:bold">has </span><span style=
=3D"color:#800080">$.debug </span><span style=3D"color:#000080;font-weight:=
bold">is </span><span style=3D"color:#4b0082">rw </span><span style=3D"colo=
r:#8b0000">=3D </span><span style=3D"color:#4b0082">False</span>;
    <span style=3D"color:#000080;font-weight:bold">has </span><span style=
=3D"color:#800080">$.value </span><span style=3D"color:#000080;font-weight:=
bold">is </span><span style=3D"color:#4b0082">rw </span><span style=3D"colo=
r:#8b0000">=3D </span><span style=3D"color:#008000;font-weight:bold">&#39;I=
nitial value&#39;</span>;

    <span style=3D"color:#000080;font-weight:bold">proto method </span><spa=
n style=3D"color:#ff4500">handle</span>( <span style=3D"color:#8b0000">|</s=
pan><span style=3D"color:#4b0082">c </span>) {
        <span style=3D"color:#ff4500">note </span><span style=3D"color:#008=
000;font-weight:bold">&quot;value is </span><span style=3D"color:#800080">$=
</span><span style=3D"color:#8b0000">.</span><span style=3D"color:#ff4500">=
value</span><span style=3D"color:#008000;font-weight:bold">&quot; </span><s=
pan style=3D"color:#000080;font-weight:bold">if </span><span style=3D"color=
:#800080">$</span><span style=3D"color:#8b0000">.</span><span style=3D"colo=
r:#ff4500">debug</span>;
        <span style=3D"color:#4b0082">{ * }
</span><span style=3D"color:#4b0082">    </span>}
    <span style=3D"color:#000080;font-weight:bold">multi method </span><spa=
n style=3D"color:#ff4500">handle</span>(<span style=3D"color:#4b0082">Str <=
/span><span style=3D"color:#800080">$s</span>) {
        <span style=3D"color:#800080">$</span><span style=3D"color:#8b0000"=
>.</span><span style=3D"color:#ff4500">value </span><span style=3D"color:#8=
b0000">=3D </span><span style=3D"color:#800080">$s</span>;
        <span style=3D"color:#ff4500">say </span><span style=3D"color:#0080=
00;font-weight:bold">&#39;in string&#39;
</span><span style=3D"color:#008000;font-weight:bold">    </span>}
    <span style=3D"color:#000080;font-weight:bold">multi method </span><spa=
n style=3D"color:#ff4500">handle</span>(<span style=3D"color:#4b0082">Posit=
ional </span><span style=3D"color:#800080">@s</span>) {
        <span style=3D"color:#800080">$</span><span style=3D"color:#8b0000"=
>.</span><span style=3D"color:#ff4500">value </span><span style=3D"color:#8=
b0000">=3D </span><span style=3D"color:#800080">@s</span>[<span style=3D"co=
lor:#0000ff">0</span>];
        <span style=3D"color:#ff4500">say </span><span style=3D"color:#0080=
00;font-weight:bold">&#39;in positional&#39;
</span><span style=3D"color:#008000;font-weight:bold">    </span>}
}

<span style=3D"color:#000080;font-weight:bold">my </span><span style=3D"col=
or:#4b0082">NewClass </span><span style=3D"color:#800080">$x </span><span s=
tyle=3D"color:#8b0000">.=3D </span><span style=3D"color:#ff4500">new</span>=
;

<span style=3D"color:#800080">$x</span><span style=3D"color:#8b0000">.</spa=
n><span style=3D"color:#ff4500">handle</span>(<span style=3D"color:#008000;=
font-weight:bold">&#39;hello world&#39;</span>);
<span style=3D"color:#800080">$x</span><span style=3D"color:#8b0000">.</spa=
n><span style=3D"color:#ff4500">handle</span>(<span style=3D"color:#008000;=
font-weight:bold">&lt;hello world&gt;</span>);
<span style=3D"color:#800080">$x</span><span style=3D"color:#8b0000">.</spa=
n><span style=3D"color:#ff4500">debug </span><span style=3D"color:#8b0000">=
=3D </span><span style=3D"color:#4b0082">True</span>;
<span style=3D"color:#800080">$x</span><span style=3D"color:#8b0000">.</spa=
n><span style=3D"color:#ff4500">handle</span>(<span style=3D"color:#008000;=
font-weight:bold">&#39;hello world&#39;</span>);
<span style=3D"color:#800080">$x</span><span style=3D"color:#8b0000">.</spa=
n><span style=3D"color:#ff4500">handle</span>(<span style=3D"color:#008000;=
font-weight:bold">&lt;hello world&gt;</span>);

#raku test.raku=20
#value is Initial value
#value is Initial value

I am wondering how to use proto and {*}
</pre>
  </div>

</blockquote></div>

--000000000000d544fa05a93c81f4--
0
not
6/29/2020 5:36:53 PM
--0000000000008f3a2105a93c9c3b
Content-Type: text/plain; charset="UTF-8"

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

--0000000000008f3a2105a93c9c3b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"color:#000000">After=
 deleting the spaces as suggested, there&#39;s a &quot;Positional&quot; too=
 many.</div><div class=3D"gmail_default" style=3D"color:#000000">I guess yo=
u can rewrite that method declaration as</div><div class=3D"gmail_default" =
style=3D"color:#000000"><br></div><div class=3D"gmail_default" style=3D"col=
or:#000000">multi method handle(@s)</div><div class=3D"gmail_default" style=
=3D"color:#000000"><br></div><div class=3D"gmail_default" style=3D"color:#0=
00000">or</div><div class=3D"gmail_default" style=3D"color:#000000"><br></d=
iv><div class=3D"gmail_default" style=3D"color:#000000">multi method handle=
(Positional $s)</div><div class=3D"gmail_default" style=3D"color:#000000"><=
br></div><div class=3D"gmail_default" style=3D"color:#000000">and adjust th=
e method&#39;s body.<br></div></div><br><div class=3D"gmail_quote"><div dir=
=3D"ltr" class=3D"gmail_attr">On Mon, Jun 29, 2020 at 7:37 PM yary &lt;<a h=
ref=3D"mailto:not.com@gmail.com">not.com@gmail.com</a>&gt; wrote:<br></div>=
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"auto">It look=
s like you have spaces in the token { * } can you try it without, using thi=
s {*} instead?</div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=
=3D"gmail_attr">On Mon, Jun 29, 2020, 1:29 PM Richard Hainsworth &lt;<a hre=
f=3D"mailto:rnhainsworth@gmail.com" target=3D"_blank">rnhainsworth@gmail.co=
m</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin=
:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"=
>
 =20

   =20
 =20
  <div>
    <p>I have several multi methods.</p>
    <p>All of them have the same first statement, then differ depending
      on the signature.</p>
    <p>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:</p>
    <p>&quot;You can give the <code>proto</code> a function body, and place
      the <code>{*}</code> where you want the dispatch to be done. This
      can be useful when you have a &quot;hole&quot; in your routine that g=
ives it
      different behavior depending on the arguments given:&quot; <br>
    </p>
    <p>The docs give and example proto, but unfortunately, not how this
      works with other multi&#39;s.<br>
    </p>
    <p>So=C2=A0 I tried this:<br>
    </p>
    <pre style=3D"background-color:rgb(255,255,255);color:rgb(0,0,0);font-f=
amily:&quot;DejaVu Sans Mono&quot;;font-size:9pt"><span style=3D"color:rgb(=
0,0,128);font-weight:bold">class </span><span style=3D"color:rgb(75,0,130)"=
>NewClass </span>{
    <span style=3D"color:rgb(0,0,128);font-weight:bold">has </span><span st=
yle=3D"color:rgb(128,0,128)">$.debug </span><span style=3D"color:rgb(0,0,12=
8);font-weight:bold">is </span><span style=3D"color:rgb(75,0,130)">rw </spa=
n><span style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(75=
,0,130)">False</span>;
    <span style=3D"color:rgb(0,0,128);font-weight:bold">has </span><span st=
yle=3D"color:rgb(128,0,128)">$.value </span><span style=3D"color:rgb(0,0,12=
8);font-weight:bold">is </span><span style=3D"color:rgb(75,0,130)">rw </spa=
n><span style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(0,=
128,0);font-weight:bold">&#39;Initial value&#39;</span>;

    <span style=3D"color:rgb(0,0,128);font-weight:bold">proto method </span=
><span style=3D"color:rgb(255,69,0)">handle</span>( <span style=3D"color:rg=
b(139,0,0)">|</span><span style=3D"color:rgb(75,0,130)">c </span>) {
        <span style=3D"color:rgb(255,69,0)">note </span><span style=3D"colo=
r:rgb(0,128,0);font-weight:bold">&quot;value is </span><span style=3D"color=
:rgb(128,0,128)">$</span><span style=3D"color:rgb(139,0,0)">.</span><span s=
tyle=3D"color:rgb(255,69,0)">value</span><span style=3D"color:rgb(0,128,0);=
font-weight:bold">&quot; </span><span style=3D"color:rgb(0,0,128);font-weig=
ht:bold">if </span><span style=3D"color:rgb(128,0,128)">$</span><span style=
=3D"color:rgb(139,0,0)">.</span><span style=3D"color:rgb(255,69,0)">debug</=
span>;
        <span style=3D"color:rgb(75,0,130)">{ * }
</span><span style=3D"color:rgb(75,0,130)">    </span>}
    <span style=3D"color:rgb(0,0,128);font-weight:bold">multi method </span=
><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=3D"color:rgb=
(75,0,130)">Str </span><span style=3D"color:rgb(128,0,128)">$s</span>) {
        <span style=3D"color:rgb(128,0,128)">$</span><span style=3D"color:r=
gb(139,0,0)">.</span><span style=3D"color:rgb(255,69,0)">value </span><span=
 style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(128,0,128=
)">$s</span>;
        <span style=3D"color:rgb(255,69,0)">say </span><span style=3D"color=
:rgb(0,128,0);font-weight:bold">&#39;in string&#39;
</span><span style=3D"color:rgb(0,128,0);font-weight:bold">    </span>}
    <span style=3D"color:rgb(0,0,128);font-weight:bold">multi method </span=
><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=3D"color:rgb=
(75,0,130)">Positional </span><span style=3D"color:rgb(128,0,128)">@s</span=
>) {
        <span style=3D"color:rgb(128,0,128)">$</span><span style=3D"color:r=
gb(139,0,0)">.</span><span style=3D"color:rgb(255,69,0)">value </span><span=
 style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(128,0,128=
)">@s</span>[<span style=3D"color:rgb(0,0,255)">0</span>];
        <span style=3D"color:rgb(255,69,0)">say </span><span style=3D"color=
:rgb(0,128,0);font-weight:bold">&#39;in positional&#39;
</span><span style=3D"color:rgb(0,128,0);font-weight:bold">    </span>}
}

<span style=3D"color:rgb(0,0,128);font-weight:bold">my </span><span style=
=3D"color:rgb(75,0,130)">NewClass </span><span style=3D"color:rgb(128,0,128=
)">$x </span><span style=3D"color:rgb(139,0,0)">.=3D </span><span style=3D"=
color:rgb(255,69,0)">new</span>;

<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&#39;hello world&#39;</span>);
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&lt;hello world&gt;</span>);
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">debug </span><span style=
=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(75,0,130)">True=
</span>;
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&#39;hello world&#39;</span>);
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&lt;hello world&gt;</span>);

#raku test.raku=20
#value is Initial value
#value is Initial value

I am wondering how to use proto and {*}
</pre>
  </div>

</blockquote></div>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature">Fernando Santagata</div>

--0000000000008f3a2105a93c9c3b--
0
nando
6/29/2020 5:44:21 PM
--------------24AD87D2F2812CA020DB3E99
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

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.

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 
> <mailto: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 <mailto: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

--------------24AD87D2F2812CA020DB3E99
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>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.</p>
    <p>b) Removing the space yields the following response.<br>
      in string<br>
      Cannot resolve caller handle(NewClass:D: List:D); none of these
      signatures match:<br>
          (NewClass: Str $s, *%_)<br>
          (NewClass: Positional @s, *%_)<br>
        in method handle at test.raku line 15<br>
        in block &lt;unit&gt; at test.raku line 30<br>
    </p>
    <p>Not sure why the List:D is not being matched to Positional. Is
      the List:D refering to the |c signature capture ??</p>
    <p>Since 'in string' was printed, one of the methods was reached.
      Confused.</p>
    <p>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. <br>
    </p>
    <p>Regards<br>
    </p>
    <div class="moz-cite-prefix">On 29/06/2020 18:44, Fernando Santagata
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAJ+jyGjFDXnsBdNSyOiKGfSxqOi-sLwdvfUQy=cZoi7sa3qr-Q@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div class="gmail_default" style="color:#000000">After deleting
          the spaces as suggested, there's a "Positional" too many.</div>
        <div class="gmail_default" style="color:#000000">I guess you can
          rewrite that method declaration as</div>
        <div class="gmail_default" style="color:#000000"><br>
        </div>
        <div class="gmail_default" style="color:#000000">multi method
          handle(@s)</div>
        <div class="gmail_default" style="color:#000000"><br>
        </div>
        <div class="gmail_default" style="color:#000000">or</div>
        <div class="gmail_default" style="color:#000000"><br>
        </div>
        <div class="gmail_default" style="color:#000000">multi method
          handle(Positional $s)</div>
        <div class="gmail_default" style="color:#000000"><br>
        </div>
        <div class="gmail_default" style="color:#000000">and adjust the
          method's body.<br>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">On Mon, Jun 29, 2020 at 7:37
          PM yary &lt;<a href="mailto:not.com@gmail.com"
            moz-do-not-send="true">not.com@gmail.com</a>&gt; wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px
          0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div dir="auto">It looks like you have spaces in the token { *
            } can you try it without, using this {*} instead?</div>
          <br>
          <div class="gmail_quote">
            <div dir="ltr" class="gmail_attr">On Mon, Jun 29, 2020, 1:29
              PM Richard Hainsworth &lt;<a
                href="mailto:rnhainsworth@gmail.com" target="_blank"
                moz-do-not-send="true">rnhainsworth@gmail.com</a>&gt;
              wrote:<br>
            </div>
            <blockquote class="gmail_quote" style="margin:0px 0px 0px
              0.8ex;border-left:1px solid
              rgb(204,204,204);padding-left:1ex">
              <div>
                <p>I have several multi methods.</p>
                <p>All of them have the same first statement, then
                  differ depending on the signature.</p>
                <p>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:</p>
                <p>"You can give the <code>proto</code> a function
                  body, and place the <code>{*}</code> 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:" <br>
                </p>
                <p>The docs give and example proto, but unfortunately,
                  not how this works with other multi's.<br>
                </p>
                <p>So  I tried this:<br>
                </p>
                <pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:&quot;DejaVu Sans Mono&quot;;font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold">class </span><span style="color:rgb(75,0,130)">NewClass </span>{
    <span style="color:rgb(0,0,128);font-weight:bold">has </span><span style="color:rgb(128,0,128)">$.debug </span><span style="color:rgb(0,0,128);font-weight:bold">is </span><span style="color:rgb(75,0,130)">rw </span><span style="color:rgb(139,0,0)">= </span><span style="color:rgb(75,0,130)">False</span>;
    <span style="color:rgb(0,0,128);font-weight:bold">has </span><span style="color:rgb(128,0,128)">$.value </span><span style="color:rgb(0,0,128);font-weight:bold">is </span><span style="color:rgb(75,0,130)">rw </span><span style="color:rgb(139,0,0)">= </span><span style="color:rgb(0,128,0);font-weight:bold">'Initial value'</span>;

    <span style="color:rgb(0,0,128);font-weight:bold">proto method </span><span style="color:rgb(255,69,0)">handle</span>( <span style="color:rgb(139,0,0)">|</span><span style="color:rgb(75,0,130)">c </span>) {
        <span style="color:rgb(255,69,0)">note </span><span style="color:rgb(0,128,0);font-weight:bold">"value is </span><span style="color:rgb(128,0,128)">$</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">value</span><span style="color:rgb(0,128,0);font-weight:bold">" </span><span style="color:rgb(0,0,128);font-weight:bold">if </span><span style="color:rgb(128,0,128)">$</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">debug</span>;
        <span style="color:rgb(75,0,130)">{ * }
</span><span style="color:rgb(75,0,130)">    </span>}
    <span style="color:rgb(0,0,128);font-weight:bold">multi method </span><span style="color:rgb(255,69,0)">handle</span>(<span style="color:rgb(75,0,130)">Str </span><span style="color:rgb(128,0,128)">$s</span>) {
        <span style="color:rgb(128,0,128)">$</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">value </span><span style="color:rgb(139,0,0)">= </span><span style="color:rgb(128,0,128)">$s</span>;
        <span style="color:rgb(255,69,0)">say </span><span style="color:rgb(0,128,0);font-weight:bold">'in string'
</span><span style="color:rgb(0,128,0);font-weight:bold">    </span>}
    <span style="color:rgb(0,0,128);font-weight:bold">multi method </span><span style="color:rgb(255,69,0)">handle</span>(<span style="color:rgb(75,0,130)">Positional </span><span style="color:rgb(128,0,128)">@s</span>) {
        <span style="color:rgb(128,0,128)">$</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">value </span><span style="color:rgb(139,0,0)">= </span><span style="color:rgb(128,0,128)">@s</span>[<span style="color:rgb(0,0,255)">0</span>];
        <span style="color:rgb(255,69,0)">say </span><span style="color:rgb(0,128,0);font-weight:bold">'in positional'
</span><span style="color:rgb(0,128,0);font-weight:bold">    </span>}
}

<span style="color:rgb(0,0,128);font-weight:bold">my </span><span style="color:rgb(75,0,130)">NewClass </span><span style="color:rgb(128,0,128)">$x </span><span style="color:rgb(139,0,0)">.= </span><span style="color:rgb(255,69,0)">new</span>;

<span style="color:rgb(128,0,128)">$x</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">handle</span>(<span style="color:rgb(0,128,0);font-weight:bold">'hello world'</span>);
<span style="color:rgb(128,0,128)">$x</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">handle</span>(<span style="color:rgb(0,128,0);font-weight:bold">&lt;hello world&gt;</span>);
<span style="color:rgb(128,0,128)">$x</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">debug </span><span style="color:rgb(139,0,0)">= </span><span style="color:rgb(75,0,130)">True</span>;
<span style="color:rgb(128,0,128)">$x</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">handle</span>(<span style="color:rgb(0,128,0);font-weight:bold">'hello world'</span>);
<span style="color:rgb(128,0,128)">$x</span><span style="color:rgb(139,0,0)">.</span><span style="color:rgb(255,69,0)">handle</span>(<span style="color:rgb(0,128,0);font-weight:bold">&lt;hello world&gt;</span>);

#raku test.raku 
#value is Initial value
#value is Initial value

I am wondering how to use proto and {*}
</pre>
              </div>
            </blockquote>
          </div>
        </blockquote>
      </div>
      <br clear="all">
      <br>
      -- <br>
      <div dir="ltr" class="gmail_signature">Fernando Santagata</div>
    </blockquote>
  </body>
</html>

--------------24AD87D2F2812CA020DB3E99--
0
rnhainsworth
6/29/2020 6:22:49 PM
On 2020-06-29 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.

Yep. Weird, but it's a special token.

> Not sure why the List:D is not being matched to Positional. Is the 
> List:D refering to the |c signature capture ??

Your method wants a `Positional @s`, which is an array/list/seq
containing only objects that do the `Positional` role. You're passing
it a list of strings. Either write `@s` (the @ implies you want a
positional thing) or `Positional $s` (a scalar containing a positional
object)

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

This works::

  class NewClass {
      has $.debug is rw = False;
      has $.value is rw = 'Initial value';

      proto method handle( |c ) {
            note "before value is $.value" if $.debug;
            {*}
            note "after value is $.value" if $.debug;
      }
      multi method handle(Str $s) {
          $.value = $s;
          say 'in string'
      }
      multi method handle(@s) {
          $.value = @s[0];
          say 'in positional'
      }
  }

  my NewClass $x .= new(:debug);

  $x.handle('hello world');
  $x.handle(<hello world>);

and prints::

  before value is Initial value
  in string
  after value is hello world
  before value is hello world
  in positional
  after value is hello

-- 
	Dakkar - <Mobilis in mobile>
	GPG public key fingerprint = A071 E618 DD2C 5901 9574
	                             6FE2 40EA 9883 7519 3F88
	                    key id = 0x75193F88
0
dakkar
6/29/2020 6:31:31 PM
--00000000000046e2bc05a93d462c
Content-Type: text/plain; charset="UTF-8"

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

--00000000000046e2bc05a93d462c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Mon, Jun 29, 2020 at 8:22 PM Richard Hainsworth &lt;<a href=3D"ma=
ilto:rnhainsworth@gmail.com">rnhainsworth@gmail.com</a>&gt; wrote:<br></div=
><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border=
-left:1px solid rgb(204,204,204);padding-left:1ex">
 =20
   =20
 =20
  <div>
    <p>a) I don&#39;t understand why the white space matters, but clearly i=
t
      does. So the token is &#39;{*}&#39; and not braces around a Whatever-=
star.</p></div></blockquote><div><div style=3D"color:rgb(0,0,0)" class=3D"g=
mail_default">For an explanation see the thread &quot;Playing with protos a=
nd phasers&quot; that I started here three days ago.</div><div> <br></div><=
/div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
    <p>b) Removing the space yields the following response.<br>
      in string<br>
      Cannot resolve caller handle(NewClass:D: List:D); none of these
      signatures match:<br>
      =C2=A0=C2=A0=C2=A0 (NewClass: Str $s, *%_)<br>
      =C2=A0=C2=A0=C2=A0 (NewClass: Positional @s, *%_)<br>
      =C2=A0 in method handle at test.raku line 15<br>
      =C2=A0 in block &lt;unit&gt; at test.raku line 30<br>
    </p>
    <p>Not sure why the List:D is not being matched to Positional. Is
      the List:D refering to the |c signature capture ??</p>
    <p>Since &#39;in string&#39; was printed, one of the methods was reache=
d.
      Confused.</p>
    <p>c) Writing out all the code in each method is what I already
      have. But I&#39;m looking for ways to factor out common code. <br>
    </p>
    <p>Regards<br>
    </p>
    <div>On 29/06/2020 18:44, Fernando Santagata
      wrote:<br>
    </div>
    <blockquote type=3D"cite">
     =20
      <div dir=3D"ltr">
        <div style=3D"color:rgb(0,0,0)">After deleting
          the spaces as suggested, there&#39;s a &quot;Positional&quot; too=
 many.</div>
        <div style=3D"color:rgb(0,0,0)">I guess you can
          rewrite that method declaration as</div>
        <div style=3D"color:rgb(0,0,0)"><br>
        </div>
        <div style=3D"color:rgb(0,0,0)">multi method
          handle(@s)</div>
        <div style=3D"color:rgb(0,0,0)"><br>
        </div>
        <div style=3D"color:rgb(0,0,0)">or</div>
        <div style=3D"color:rgb(0,0,0)"><br>
        </div>
        <div style=3D"color:rgb(0,0,0)">multi method
          handle(Positional $s)</div>
        <div style=3D"color:rgb(0,0,0)"><br>
        </div>
        <div style=3D"color:rgb(0,0,0)">and adjust the
          method&#39;s body.<br>
        </div>
      </div>
      <br>
      <div class=3D"gmail_quote">
        <div dir=3D"ltr" class=3D"gmail_attr">On Mon, Jun 29, 2020 at 7:37
          PM yary &lt;<a href=3D"mailto:not.com@gmail.com" target=3D"_blank=
">not.com@gmail.com</a>&gt; wrote:<br>
        </div>
        <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex=
;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div dir=3D"auto">It looks like you have spaces in the token { *
            } can you try it without, using this {*} instead?</div>
          <br>
          <div class=3D"gmail_quote">
            <div dir=3D"ltr" class=3D"gmail_attr">On Mon, Jun 29, 2020, 1:2=
9
              PM Richard Hainsworth &lt;<a href=3D"mailto:rnhainsworth@gmai=
l.com" target=3D"_blank">rnhainsworth@gmail.com</a>&gt;
              wrote:<br>
            </div>
            <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0=
..8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
              <div>
                <p>I have several multi methods.</p>
                <p>All of them have the same first statement, then
                  differ depending on the signature.</p>
                <p>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:</p>
                <p>&quot;You can give the <code>proto</code> a function
                  body, and place the <code>{*}</code> where you want
                  the dispatch to be done. This can be useful when you
                  have a &quot;hole&quot; in your routine that gives it dif=
ferent
                  behavior depending on the arguments given:&quot; <br>
                </p>
                <p>The docs give and example proto, but unfortunately,
                  not how this works with other multi&#39;s.<br>
                </p>
                <p>So=C2=A0 I tried this:<br>
                </p>
                <pre style=3D"background-color:rgb(255,255,255);color:rgb(0=
,0,0);font-family:&quot;DejaVu Sans Mono&quot;;font-size:9pt"><span style=
=3D"color:rgb(0,0,128);font-weight:bold">class </span><span style=3D"color:=
rgb(75,0,130)">NewClass </span>{
    <span style=3D"color:rgb(0,0,128);font-weight:bold">has </span><span st=
yle=3D"color:rgb(128,0,128)">$.debug </span><span style=3D"color:rgb(0,0,12=
8);font-weight:bold">is </span><span style=3D"color:rgb(75,0,130)">rw </spa=
n><span style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(75=
,0,130)">False</span>;
    <span style=3D"color:rgb(0,0,128);font-weight:bold">has </span><span st=
yle=3D"color:rgb(128,0,128)">$.value </span><span style=3D"color:rgb(0,0,12=
8);font-weight:bold">is </span><span style=3D"color:rgb(75,0,130)">rw </spa=
n><span style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(0,=
128,0);font-weight:bold">&#39;Initial value&#39;</span>;

    <span style=3D"color:rgb(0,0,128);font-weight:bold">proto method </span=
><span style=3D"color:rgb(255,69,0)">handle</span>( <span style=3D"color:rg=
b(139,0,0)">|</span><span style=3D"color:rgb(75,0,130)">c </span>) {
        <span style=3D"color:rgb(255,69,0)">note </span><span style=3D"colo=
r:rgb(0,128,0);font-weight:bold">&quot;value is </span><span style=3D"color=
:rgb(128,0,128)">$</span><span style=3D"color:rgb(139,0,0)">.</span><span s=
tyle=3D"color:rgb(255,69,0)">value</span><span style=3D"color:rgb(0,128,0);=
font-weight:bold">&quot; </span><span style=3D"color:rgb(0,0,128);font-weig=
ht:bold">if </span><span style=3D"color:rgb(128,0,128)">$</span><span style=
=3D"color:rgb(139,0,0)">.</span><span style=3D"color:rgb(255,69,0)">debug</=
span>;
        <span style=3D"color:rgb(75,0,130)">{ * }
</span><span style=3D"color:rgb(75,0,130)">    </span>}
    <span style=3D"color:rgb(0,0,128);font-weight:bold">multi method </span=
><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=3D"color:rgb=
(75,0,130)">Str </span><span style=3D"color:rgb(128,0,128)">$s</span>) {
        <span style=3D"color:rgb(128,0,128)">$</span><span style=3D"color:r=
gb(139,0,0)">.</span><span style=3D"color:rgb(255,69,0)">value </span><span=
 style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(128,0,128=
)">$s</span>;
        <span style=3D"color:rgb(255,69,0)">say </span><span style=3D"color=
:rgb(0,128,0);font-weight:bold">&#39;in string&#39;
</span><span style=3D"color:rgb(0,128,0);font-weight:bold">    </span>}
    <span style=3D"color:rgb(0,0,128);font-weight:bold">multi method </span=
><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=3D"color:rgb=
(75,0,130)">Positional </span><span style=3D"color:rgb(128,0,128)">@s</span=
>) {
        <span style=3D"color:rgb(128,0,128)">$</span><span style=3D"color:r=
gb(139,0,0)">.</span><span style=3D"color:rgb(255,69,0)">value </span><span=
 style=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(128,0,128=
)">@s</span>[<span style=3D"color:rgb(0,0,255)">0</span>];
        <span style=3D"color:rgb(255,69,0)">say </span><span style=3D"color=
:rgb(0,128,0);font-weight:bold">&#39;in positional&#39;
</span><span style=3D"color:rgb(0,128,0);font-weight:bold">    </span>}
}

<span style=3D"color:rgb(0,0,128);font-weight:bold">my </span><span style=
=3D"color:rgb(75,0,130)">NewClass </span><span style=3D"color:rgb(128,0,128=
)">$x </span><span style=3D"color:rgb(139,0,0)">.=3D </span><span style=3D"=
color:rgb(255,69,0)">new</span>;

<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&#39;hello world&#39;</span>);
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&lt;hello world&gt;</span>);
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">debug </span><span style=
=3D"color:rgb(139,0,0)">=3D </span><span style=3D"color:rgb(75,0,130)">True=
</span>;
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&#39;hello world&#39;</span>);
<span style=3D"color:rgb(128,0,128)">$x</span><span style=3D"color:rgb(139,=
0,0)">.</span><span style=3D"color:rgb(255,69,0)">handle</span>(<span style=
=3D"color:rgb(0,128,0);font-weight:bold">&lt;hello world&gt;</span>);

#raku test.raku=20
#value is Initial value
#value is Initial value

I am wondering how to use proto and {*}
</pre>
              </div>
            </blockquote>
          </div>
        </blockquote>
      </div>
      <br clear=3D"all">
      <br>
      -- <br>
      <div dir=3D"ltr">Fernando Santagata</div>
    </blockquote>
  </div>

</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature">Fernando Santagata</div></div>

--00000000000046e2bc05a93d462c--
0
nando
6/29/2020 6:31:49 PM
@Gianni - thank you.

That works.

The problem in my original code - after removing the whitespace in { * } 
- was to have 'Positional @s' instead of 'Positional $s'. Either '@s' or 
'Positional $s' work.

Thanks for the rapid feedback.

Richard

On 29/06/2020 19:31, Gianni Ceccarelli wrote:
> On 2020-06-29 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.
> Yep. Weird, but it's a special token.
>
>> Not sure why the List:D is not being matched to Positional. Is the
>> List:D refering to the |c signature capture ??
> Your method wants a `Positional @s`, which is an array/list/seq
> containing only objects that do the `Positional` role. You're passing
> it a list of strings. Either write `@s` (the @ implies you want a
> positional thing) or `Positional $s` (a scalar containing a positional
> object)
>
>> 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.
> This works::
>
>    class NewClass {
>        has $.debug is rw = False;
>        has $.value is rw = 'Initial value';
>
>        proto method handle( |c ) {
>              note "before value is $.value" if $.debug;
>              {*}
>              note "after value is $.value" if $.debug;
>        }
>        multi method handle(Str $s) {
>            $.value = $s;
>            say 'in string'
>        }
>        multi method handle(@s) {
>            $.value = @s[0];
>            say 'in positional'
>        }
>    }
>
>    my NewClass $x .= new(:debug);
>
>    $x.handle('hello world');
>    $x.handle(<hello world>);
>
> and prints::
>
>    before value is Initial value
>    in string
>    after value is hello world
>    before value is hello world
>    in positional
>    after value is hello
>
0
rnhainsworth
6/29/2020 6:50:46 PM
Reply: