Playing with protos and phasers

--00000000000071033e05a8e3ce4f
Content-Type: text/plain; charset="UTF-8"

Hi *,
I was trying to see if one can delegate phasers to a proto, like this:

proto sub foo(|) {
  ENTER { say 'In' }
  LEAVE { say 'Out' }
  {*}
}
multi sub foo(Int $a) {
  $a
}
multi sub foo(Str $a) {
  $a
}

say foo(1);
say foo('hello');

Yes indeed, it outputs:

In
Out
1
In
Out
hello

But at first it didn't work, because I added a space on both sides of the
'*':

proto sub foo(|) {
  ENTER { say 'In' }
  LEAVE { say 'Out' }
  { * }
}

It outputs:

In
Out
*
In
Out
*

If I add a return:

proto sub foo(|) {
  ENTER { say 'In' }
  LEAVE { say 'Out' }
  return { * }
}

it outputs:

In
Out
-> ;; $_? is raw = OUTER::<$_> { #`(Block|94038346387904) ... }
In
Out
-> ;; $_? is raw = OUTER::<$_> { #`(Block|94038346388120) ... }

Why are spaces significant in this case?

If this behavior is intentional, can this be a trap worth being documented?

-- 
Fernando Santagata

--00000000000071033e05a8e3ce4f
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">Hi *,=
</div><div class=3D"gmail_default" style=3D"color:#000000">I was trying to =
see if one can delegate phasers to a proto, like this:</div><div class=3D"g=
mail_default" style=3D"color:#000000"><br></div><div class=3D"gmail_default=
" style=3D"color:#000000">proto sub foo(|) {<br>=C2=A0 ENTER { say &#39;In&=
#39; }<br>=C2=A0 LEAVE { say &#39;Out&#39; }<br>=C2=A0 {*}<br>}<br>multi su=
b foo(Int $a) {<br>=C2=A0 $a<br>}<br>multi sub foo(Str $a) {<br>=C2=A0 $a<b=
r>}<br><br>say foo(1);<br>say foo(&#39;hello&#39;);</div><div class=3D"gmai=
l_default" style=3D"color:#000000"><br></div><div class=3D"gmail_default" s=
tyle=3D"color:#000000">Yes indeed, it outputs:</div><div class=3D"gmail_def=
ault" style=3D"color:#000000"><br></div><div class=3D"gmail_default" style=
=3D"color:#000000">In<br>Out<br>1<br>In<br>Out<br>hello</div><div class=3D"=
gmail_default" style=3D"color:#000000"><br></div><div class=3D"gmail_defaul=
t" style=3D"color:#000000">But at first it didn&#39;t work, because I added=
 a space on both sides of the &#39;*&#39;:</div><div class=3D"gmail_default=
" style=3D"color:#000000"><br></div><div class=3D"gmail_default" style=3D"c=
olor:#000000">proto sub foo(|) {<br>=C2=A0 ENTER { say &#39;In&#39; }<br>=
=C2=A0 LEAVE { say &#39;Out&#39; }<br>=C2=A0 { * }<br>}</div><div><br></div=
><div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">It outputs:</=
div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"><br></div><div =
style=3D"color:rgb(0,0,0)" class=3D"gmail_default">In<br>Out<br>*<br>In<br>=
Out<br>*</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"><br><=
/div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">If I add a ret=
urn:</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"><br></div=
><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">proto sub foo(|) {=
<br>=C2=A0 ENTER { say &#39;In&#39; }<br>=C2=A0 LEAVE { say &#39;Out&#39; }=
<br>=C2=A0 return { * }<br>}</div><div style=3D"color:rgb(0,0,0)" class=3D"=
gmail_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_def=
ault">it outputs:</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_defau=
lt"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">In<br=
>Out<br>-&gt; ;; $_? is raw =3D OUTER::&lt;$_&gt; { #`(Block|94038346387904=
) ... }<br>In<br>Out<br>-&gt; ;; $_? is raw =3D OUTER::&lt;$_&gt; { #`(Bloc=
k|94038346388120) ... }</div></div><div><br></div><div><div style=3D"color:=
rgb(0,0,0)" class=3D"gmail_default">Why are spaces significant in this case=
?<br></div></div><div><br></div><div><div style=3D"color:rgb(0,0,0)" class=
=3D"gmail_default">If this behavior is intentional, can this be a trap wort=
h being documented?<br></div></div><div><br></div>-- <br><div dir=3D"ltr" c=
lass=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santag=
ata</div></div>

--00000000000071033e05a8e3ce4f--
0
nando
6/25/2020 7:47:26 AM
perl.perl6.users 1426 articles. 0 followers. Follow

3 Replies
21 Views

Similar Articles

[PageSpeed] 13

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

{*} is specially handled by the compiler as a term.

Let's say you have a class named Foo:

    class Foo {=E2=80=A6}

You wouldn't expect to be able to use it like this:

    F o o.new()

It is the same thing with {*}.
The only difference is that {*} is meant to look like a Block { } combined
with a Whatever *.
It was meant to be both visually distinctive, and visually similar to how
you might think about it.

---

If you were allowed to add spaces to the {*} term, it would be annoying if
you wanted to create a lambda that returned a Whatever.

    my $block =3D { * };

    say $block.() ~~ Whatever; # True.

On Thu, Jun 25, 2020 at 2:47 AM Fernando Santagata <
nando.santagata@gmail.com> wrote:

> Hi *,
> I was trying to see if one can delegate phasers to a proto, like this:
>
> proto sub foo(|) {
>   ENTER { say 'In' }
>   LEAVE { say 'Out' }
>   {*}
> }
> multi sub foo(Int $a) {
>   $a
> }
> multi sub foo(Str $a) {
>   $a
> }
>
> say foo(1);
> say foo('hello');
>
> Yes indeed, it outputs:
>
> In
> Out
> 1
> In
> Out
> hello
>
> But at first it didn't work, because I added a space on both sides of the
> '*':
>
> proto sub foo(|) {
>   ENTER { say 'In' }
>   LEAVE { say 'Out' }
>   { * }
> }
>
> It outputs:
>
> In
> Out
> *
> In
> Out
> *
>
> If I add a return:
>
> proto sub foo(|) {
>   ENTER { say 'In' }
>   LEAVE { say 'Out' }
>   return { * }
> }
>
> it outputs:
>
> In
> Out
> -> ;; $_? is raw =3D OUTER::<$_> { #`(Block|94038346387904) ... }
> In
> Out
> -> ;; $_? is raw =3D OUTER::<$_> { #`(Block|94038346388120) ... }
>
> Why are spaces significant in this case?
>
> If this behavior is intentional, can this be a trap worth being documente=
d?
>
> --
> Fernando Santagata
>

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

<div dir=3D"ltr">{*} is specially handled by the compiler as a term.<div><b=
r></div><div>Let&#39;s say you have a class named Foo:</div><div><br></div>=
<div>=C2=A0 =C2=A0 class Foo {=E2=80=A6}</div><div><br></div><div>You would=
n&#39;t expect to be able to use it like this:</div><div><br></div><div>=C2=
=A0 =C2=A0 F o o.new()</div><div><br></div><div>It is the same thing with {=
*}.</div><div>The only difference is that {*} is meant to look like a Block=
 { } combined with a Whatever *.</div><div>It was meant to be both visually=
 distinctive, and visually similar to how you might think about it.</div><d=
iv><br></div><div>---</div><div><br></div><div>If you were allowed to add s=
paces to the {*} term, it would be annoying if you wanted to create a lambd=
a that returned a Whatever.</div><div><br></div><div>=C2=A0 =C2=A0 my $bloc=
k =3D { * };</div><div><br></div><div>=C2=A0 =C2=A0 say $block.() ~~ Whatev=
er; # True.</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" clas=
s=3D"gmail_attr">On Thu, Jun 25, 2020 at 2:47 AM Fernando Santagata &lt;<a =
href=3D"mailto:nando.santagata@gmail.com">nando.santagata@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"ltr"><div class=3D"gmail_default" style=3D"color:rgb(0,0,0)">Hi *,</div=
><div class=3D"gmail_default" style=3D"color:rgb(0,0,0)">I was trying to se=
e if one can delegate phasers to a proto, like this:</div><div class=3D"gma=
il_default" style=3D"color:rgb(0,0,0)"><br></div><div class=3D"gmail_defaul=
t" style=3D"color:rgb(0,0,0)">proto sub foo(|) {<br>=C2=A0 ENTER { say &#39=
;In&#39; }<br>=C2=A0 LEAVE { say &#39;Out&#39; }<br>=C2=A0 {*}<br>}<br>mult=
i sub foo(Int $a) {<br>=C2=A0 $a<br>}<br>multi sub foo(Str $a) {<br>=C2=A0 =
$a<br>}<br><br>say foo(1);<br>say foo(&#39;hello&#39;);</div><div class=3D"=
gmail_default" style=3D"color:rgb(0,0,0)"><br></div><div class=3D"gmail_def=
ault" style=3D"color:rgb(0,0,0)">Yes indeed, it outputs:</div><div class=3D=
"gmail_default" style=3D"color:rgb(0,0,0)"><br></div><div class=3D"gmail_de=
fault" style=3D"color:rgb(0,0,0)">In<br>Out<br>1<br>In<br>Out<br>hello</div=
><div class=3D"gmail_default" style=3D"color:rgb(0,0,0)"><br></div><div cla=
ss=3D"gmail_default" style=3D"color:rgb(0,0,0)">But at first it didn&#39;t =
work, because I added a space on both sides of the &#39;*&#39;:</div><div c=
lass=3D"gmail_default" style=3D"color:rgb(0,0,0)"><br></div><div class=3D"g=
mail_default" style=3D"color:rgb(0,0,0)">proto sub foo(|) {<br>=C2=A0 ENTER=
 { say &#39;In&#39; }<br>=C2=A0 LEAVE { say &#39;Out&#39; }<br>=C2=A0 { * }=
<br>}</div><div><br></div><div><div style=3D"color:rgb(0,0,0)" class=3D"gma=
il_default">It outputs:</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail=
_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"=
>In<br>Out<br>*<br>In<br>Out<br>*</div><div style=3D"color:rgb(0,0,0)" clas=
s=3D"gmail_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmai=
l_default">If I add a return:</div><div style=3D"color:rgb(0,0,0)" class=3D=
"gmail_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_de=
fault">proto sub foo(|) {<br>=C2=A0 ENTER { say &#39;In&#39; }<br>=C2=A0 LE=
AVE { say &#39;Out&#39; }<br>=C2=A0 return { * }<br>}</div><div style=3D"co=
lor:rgb(0,0,0)" class=3D"gmail_default"><br></div><div style=3D"color:rgb(0=
,0,0)" class=3D"gmail_default">it outputs:</div><div style=3D"color:rgb(0,0=
,0)" class=3D"gmail_default"><br></div><div style=3D"color:rgb(0,0,0)" clas=
s=3D"gmail_default">In<br>Out<br>-&gt; ;; $_? is raw =3D OUTER::&lt;$_&gt; =
{ #`(Block|94038346387904) ... }<br>In<br>Out<br>-&gt; ;; $_? is raw =3D OU=
TER::&lt;$_&gt; { #`(Block|94038346388120) ... }</div></div><div><br></div>=
<div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">Why are spaces=
 significant in this case?<br></div></div><div><br></div><div><div style=3D=
"color:rgb(0,0,0)" class=3D"gmail_default">If this behavior is intentional,=
 can this be a trap worth being documented?<br></div></div><div><br></div>-=
- <br><div dir=3D"ltr">Fernando Santagata</div></div>
</blockquote></div>

--000000000000de29df05a8ec80b4--
0
b2gills
6/25/2020 6:09:59 PM
--0000000000008a91fd05a8eeb6c7
Content-Type: text/plain; charset="UTF-8"

On Thu, Jun 25, 2020 at 8:10 PM Brad Gilbert <b2gills@gmail.com> wrote:

> {*} is specially handled by the compiler as a term.
>

Thank you!

I guess that handling is peculiar to proto, and maybe it's worth
documenting it.

-- 
Fernando Santagata

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

<div dir=3D"ltr"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"col=
or:#000000"><span style=3D"color:rgb(34,34,34)">On Thu, Jun 25, 2020 at 8:1=
0 PM Brad Gilbert &lt;<a href=3D"mailto:b2gills@gmail.com">b2gills@gmail.co=
m</a>&gt; wrote:</span><br></div></div><div class=3D"gmail_quote"><blockquo=
te 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"ltr">{*} is specially =
handled by the compiler as a term.</div></blockquote><div><br></div><div><d=
iv class=3D"gmail_default" style=3D"color:rgb(0,0,0)">Thank you!</div></div=
><div class=3D"gmail_default" style=3D"color:rgb(0,0,0)"><br></div><div cla=
ss=3D"gmail_default" style=3D"color:rgb(0,0,0)">I guess that handling is pe=
culiar to proto, and maybe it&#39;s worth documenting it.</div></div><div><=
br></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature">Fernando Santaga=
ta</div></div>

--0000000000008a91fd05a8eeb6c7--
0
nando
6/25/2020 8:48:10 PM
--000000000000ddfe0605a8f11eaf
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

While {*} is only useful in a proto, the compiler detects it no matter
where you write it.

    > my $block =3D {*};
    =3D=3D=3DSORRY!=3D=3D=3D Error while compiling:
    {*} may only appear in proto
    at line 2
    ------> <BOL>=E2=8F=8F<EOL>

On Thu, Jun 25, 2020 at 3:48 PM Fernando Santagata <
nando.santagata@gmail.com> wrote:

> On Thu, Jun 25, 2020 at 8:10 PM Brad Gilbert <b2gills@gmail.com> wrote:
>
>> {*} is specially handled by the compiler as a term.
>>
>
> Thank you!
>
> I guess that handling is peculiar to proto, and maybe it's worth
> documenting it.
>
> --
> Fernando Santagata
>

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

<div dir=3D"ltr">While {*} is only useful in a proto, the compiler detects =
it no matter where you write it.<div><br></div><div>=C2=A0 =C2=A0 &gt; my $=
block =3D {*};<br>=C2=A0 =C2=A0 =3D=3D=3DSORRY!=3D=3D=3D Error while compil=
ing:<br>=C2=A0 =C2=A0 {*} may only appear in proto<br>=C2=A0 =C2=A0 at line=
 2<br>=C2=A0 =C2=A0 ------&gt; &lt;BOL&gt;=E2=8F=8F&lt;EOL&gt;<br></div></d=
iv><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On =
Thu, Jun 25, 2020 at 3:48 PM Fernando Santagata &lt;<a href=3D"mailto:nando=
..santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br></div><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-lef=
t:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div dir=3D=
"ltr"><div class=3D"gmail_default" style=3D"color:rgb(0,0,0)"><span style=
=3D"color:rgb(34,34,34)">On Thu, Jun 25, 2020 at 8:10 PM Brad Gilbert &lt;<=
a href=3D"mailto:b2gills@gmail.com" target=3D"_blank">b2gills@gmail.com</a>=
&gt; wrote:</span><br></div></div><div class=3D"gmail_quote"><blockquote cl=
ass=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"ltr">{*} is specially handl=
ed by the compiler as a term.</div></blockquote><div><br></div><div><div cl=
ass=3D"gmail_default" style=3D"color:rgb(0,0,0)">Thank you!</div></div><div=
 class=3D"gmail_default" style=3D"color:rgb(0,0,0)"><br></div><div class=3D=
"gmail_default" style=3D"color:rgb(0,0,0)">I guess that handling is peculia=
r to proto, and maybe it&#39;s worth documenting it.</div></div><div><br></=
div>-- <br><div dir=3D"ltr">Fernando Santagata</div></div>
</blockquote></div>

--000000000000ddfe0605a8f11eaf--
0
b2gills
6/25/2020 11:40:31 PM
Reply: