The equivalent of Moose's "around"

--001a1149354e5ac059055df469d0
Content-Type: text/plain; charset="UTF-8"

Hello,

I'm converting a program from Perl5/Moose.
I have several classes, each has some attributes that need to be processed
in the same way before being passed to other objects.

When I was using Moose, I had some "around" methods that would
automatically modify the value before delivering it to those attributes, so
delegating the object to do the needed adjustments.

Stripped to the bare bones, the thing that in Perl6 looks like this:

class A {
  has $!a;

  method a($val?)
  {
    if $val.defined {
      # Modify $val in some way
      $!a = $val;
    } else {
      $!a;
    }
  }
}

my A $a .= new;
# $a.a = 42; # This outputs an error
$a.a(42);
say $a.a;

Any hint how to make it work as an assignment, instead of a method call?
Better yet, is there a way to abstract that behavior in a role?

-- 
Fernando Santagata

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

<div dir=3D"ltr">Hello,<div><br></div><div>I&#39;m converting a program fro=
m Perl5/Moose.</div><div>I have several classes, each has some attributes t=
hat need to be processed in the same way before being passed to other objec=
ts.</div><div><br></div><div>When I was using Moose, I had some &quot;aroun=
d&quot; methods that would automatically modify the value before delivering=
 it to those attributes, so delegating the object to do the needed adjustme=
nts.</div><div><br></div><div>Stripped to the bare bones, the thing that in=
 Perl6 looks like this:</div><div><br></div><div><div>class A {</div><div>=
=C2=A0 has $!a;</div><div><br></div><div>=C2=A0 method a($val?)</div><div>=
=C2=A0 {</div><div>=C2=A0 =C2=A0 if $val.defined {</div><div>=C2=A0 =C2=A0 =
=C2=A0 # Modify $val in some way</div><div>=C2=A0 =C2=A0 =C2=A0 $!a =3D $va=
l;</div><div>=C2=A0 =C2=A0 } else {</div><div>=C2=A0 =C2=A0 =C2=A0 $!a;</di=
v><div>=C2=A0 =C2=A0 }</div><div>=C2=A0 }</div><div>}</div><div><br></div><=
div>my A $a .=3D new;</div><div># $a.a =3D 42; # This outputs an error</div=
><div>$a.a(42);</div><div>say $a.a;</div><div><br></div><div>Any hint how t=
o make it work as an assignment, instead of a method call?</div><div>Better=
 yet, is there a way to abstract that behavior in a role?</div><div><br></d=
iv>-- <br><div class=3D"gmail_signature">Fernando Santagata</div>
</div></div>

--001a1149354e5ac059055df469d0--
0
nando
11/14/2017 5:06:44 PM
perl.perl6.users 899 articles. 0 followers. Follow

19 Replies
21 Views

Similar Articles

[PageSpeed] 52

> On 14 Nov 2017, at 18:06, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> I'm converting a program from Perl5/Moose.
> I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.
>=20
> When I was using Moose, I had some "around" methods that would =
automatically modify the value before delivering it to those attributes, =
so delegating the object to do the needed adjustments.
>=20
> Stripped to the bare bones, the thing that in Perl6 looks like this:
>=20
> class A {
>   has $!a;
>=20
>   method a($val?)
>   {
>     if $val.defined {
>       # Modify $val in some way
>       $!a =3D $val;
>     } else {
>       $!a;
>     }
>   }
> }
>=20
> my A $a .=3D new;
> # $a.a =3D 42; # This outputs an error
> $a.a(42);
> say $a.a;
>=20
> Any hint how to make it work as an assignment, instead of a method =
call?
> Better yet, is there a way to abstract that behavior in a role?

I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?

class A {
    has $.a is rw;
}
my $obj =3D A.new;
$obj.a =3D 42;
dd $obj;
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
A $obj =3D A.new(a =3D> 42)



Liz=
0
liz
11/14/2017 5:11:48 PM
--001a113536b27f60f5055df509e2
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Liz,

What I need is to preprocess the value before assigning it to an attribute.

I would do that in Perl5/Moose, using "around", like this:

package A;
use Moose;

has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');

around [qw(attribute)] =3D> sub {
  my ($next, $self, $val) =3D @_;
  return $self->$next unless $val;
  return $self->$next(preprocess $val); # Preprocess the value before the
assignment
}

In this way I don't have to make an explicit call to the preprocessor any
time I assign a value to that attribute, effectively removing that from the
main program.

I'm looking for a way to do that in Perl6.

On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl> wrote=
:

> > On 14 Nov 2017, at 18:06, Fernando Santagata <nando.santagata@gmail.com=
>
> wrote:
> > I'm converting a program from Perl5/Moose.
> > I have several classes, each has some attributes that need to be
> processed in the same way before being passed to other objects.
> >
> > When I was using Moose, I had some "around" methods that would
> automatically modify the value before delivering it to those attributes, =
so
> delegating the object to do the needed adjustments.
> >
> > Stripped to the bare bones, the thing that in Perl6 looks like this:
> >
> > class A {
> >   has $!a;
> >
> >   method a($val?)
> >   {
> >     if $val.defined {
> >       # Modify $val in some way
> >       $!a =3D $val;
> >     } else {
> >       $!a;
> >     }
> >   }
> > }
> >
> > my A $a .=3D new;
> > # $a.a =3D 42; # This outputs an error
> > $a.a(42);
> > say $a.a;
> >
> > Any hint how to make it work as an assignment, instead of a method call=
?
> > Better yet, is there a way to abstract that behavior in a role?
>
> I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
>
> class A {
>     has $.a is rw;
> }
> my $obj =3D A.new;
> $obj.a =3D 42;
> dd $obj;
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> A $obj =3D A.new(a =3D> 42)
>
>
>
> Liz




--=20
Fernando Santagata

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

<div dir=3D"ltr">Hi Liz,<div><br></div><div>What I need is to preprocess th=
e value before assigning it to an attribute.</div><div><br></div><div>I wou=
ld do that in Perl5/Moose, using &quot;around&quot;, like this:</div><div><=
br></div><div>package A;</div><div>use Moose;</div><div><br></div><div>has =
&#39;attribute&#39; =3D&gt;=C2=A0(is =3D&gt; &#39;rw&#39;, isa =3D&gt; &#39=
;Str&#39;);</div><div><br></div><div>around [qw(attribute)] =3D&gt; sub {</=
div><div><div>=C2=A0 my ($next, $self, $val) =3D @_;</div><div>=C2=A0 retur=
n $self-&gt;$next unless $val;</div><div>=C2=A0 return $self-&gt;$next(prep=
rocess $val); # Preprocess the value before the assignment</div></div><div>=
}</div><div><br></div><div>In this way I don&#39;t have to make an explicit=
 call to the preprocessor any time I assign a value to that attribute, effe=
ctively removing that from the main program.</div><div><br></div><div>I&#39=
;m looking for a way to do that in Perl6.</div></div><div class=3D"gmail_ex=
tra"><br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 6:11 PM, Elizab=
eth Mattijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" targe=
t=3D"_blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><span class=3D"">&gt; On 14 Nov 2017, at 18:06, Fernando Santagata=
 &lt;<a href=3D"mailto:nando.santagata@gmail.com">nando.santagata@gmail.com=
</a>&gt; wrote:<br>
&gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; I have several classes, each has some attributes that need to be proce=
ssed in the same way before being passed to other objects.<br>
&gt;<br>
&gt; When I was using Moose, I had some &quot;around&quot; methods that wou=
ld automatically modify the value before delivering it to those attributes,=
 so delegating the object to do the needed adjustments.<br>
&gt;<br>
&gt; Stripped to the bare bones, the thing that in Perl6 looks like this:<b=
r>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0has $!a;<br>
&gt;<br>
&gt;=C2=A0 =C2=A0method a($val?)<br>
&gt;=C2=A0 =C2=A0{<br>
&gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0}<br>
&gt; }<br>
&gt;<br>
&gt; my A $a .=3D new;<br>
&gt; # $a.a =3D 42; # This outputs an error<br>
&gt; $a.a(42);<br>
&gt; say $a.a;<br>
&gt;<br>
&gt; Any hint how to make it work as an assignment, instead of a method cal=
l?<br>
&gt; Better yet, is there a way to abstract that behavior in a role?<br>
<br>
</span>I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $.a is rw;<br>
}<br>
my $obj =3D A.new;<br>
$obj.a =3D 42;<br>
dd $obj;<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
A $obj =3D A.new(a =3D&gt; 42)<br>
<br>
<br>
<br>
Liz</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata=
</div>
</div>

--001a113536b27f60f5055df509e2--
0
nando
11/14/2017 5:51:30 PM
--94eb2c063e647a9746055df5266d
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Fernando, this list recently had a discussion on object creation, and all
various ways the attributes are checked and set by the various stages. From
that, what you want is TWEAK, or perhaps BUILD, which let you do things
with the attribute values when the object is created. I leave the research
to you (or more verbose, and wiser, list members)...I'm too buuusy!

-y

On Tue, Nov 14, 2017 at 6:51 PM, Fernando Santagata <
nando.santagata@gmail.com> wrote:

> Hi Liz,
>
> What I need is to preprocess the value before assigning it to an attribut=
e.
>
> I would do that in Perl5/Moose, using "around", like this:
>
> package A;
> use Moose;
>
> has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
>
> around [qw(attribute)] =3D> sub {
>   my ($next, $self, $val) =3D @_;
>   return $self->$next unless $val;
>   return $self->$next(preprocess $val); # Preprocess the value before the
> assignment
> }
>
> In this way I don't have to make an explicit call to the preprocessor any
> time I assign a value to that attribute, effectively removing that from t=
he
> main program.
>
> I'm looking for a way to do that in Perl6.
>
> On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
> wrote:
>
>> > On 14 Nov 2017, at 18:06, Fernando Santagata <nando.santagata@gmail.co=
m>
>> wrote:
>> > I'm converting a program from Perl5/Moose.
>> > I have several classes, each has some attributes that need to be
>> processed in the same way before being passed to other objects.
>> >
>> > When I was using Moose, I had some "around" methods that would
>> automatically modify the value before delivering it to those attributes,=
 so
>> delegating the object to do the needed adjustments.
>> >
>> > Stripped to the bare bones, the thing that in Perl6 looks like this:
>> >
>> > class A {
>> >   has $!a;
>> >
>> >   method a($val?)
>> >   {
>> >     if $val.defined {
>> >       # Modify $val in some way
>> >       $!a =3D $val;
>> >     } else {
>> >       $!a;
>> >     }
>> >   }
>> > }
>> >
>> > my A $a .=3D new;
>> > # $a.a =3D 42; # This outputs an error
>> > $a.a(42);
>> > say $a.a;
>> >
>> > Any hint how to make it work as an assignment, instead of a method cal=
l?
>> > Better yet, is there a way to abstract that behavior in a role?
>>
>> I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
>>
>> class A {
>>     has $.a is rw;
>> }
>> my $obj =3D A.new;
>> $obj.a =3D 42;
>> dd $obj;
>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>> A $obj =3D A.new(a =3D> 42)
>>
>>
>>
>> Liz
>
>
>
>
> --
> Fernando Santagata
>

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

<div dir=3D"ltr">Fernando, this list recently had a discussion on object cr=
eation, and all various ways the attributes are checked and set by the vari=
ous stages. From that, what you want is TWEAK, or perhaps BUILD, which let =
you do things with the attribute values when the object is created. I leave=
 the research to you (or more verbose, and wiser, list members)...I&#39;m t=
oo buuusy!</div><div class=3D"gmail_extra"><br clear=3D"all"><div><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature">-y<br></div></div>
<br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 6:51 PM, Fernando Sa=
ntagata <span dir=3D"ltr">&lt;<a href=3D"mailto:nando.santagata@gmail.com" =
target=3D"_blank">nando.santagata@gmail.com</a>&gt;</span> wrote:<br><block=
quote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc=
 solid;padding-left:1ex"><div dir=3D"ltr">Hi Liz,<div><br></div><div>What I=
 need is to preprocess the value before assigning it to an attribute.</div>=
<div><br></div><div>I would do that in Perl5/Moose, using &quot;around&quot=
;, like this:</div><div><br></div><div>package A;</div><div>use Moose;</div=
><div><br></div><div>has &#39;attribute&#39; =3D&gt;=C2=A0(is =3D&gt; &#39;=
rw&#39;, isa =3D&gt; &#39;Str&#39;);</div><div><br></div><div>around [qw(at=
tribute)] =3D&gt; sub {</div><div><div>=C2=A0 my ($next, $self, $val) =3D @=
_;</div><div>=C2=A0 return $self-&gt;$next unless $val;</div><div>=C2=A0 re=
turn $self-&gt;$next(preprocess $val); # Preprocess the value before the as=
signment</div></div><div>}</div><div><br></div><div>In this way I don&#39;t=
 have to make an explicit call to the preprocessor any time I assign a valu=
e to that attribute, effectively removing that from the main program.</div>=
<div><br></div><div>I&#39;m looking for a way to do that in Perl6.</div></d=
iv><div class=3D"gmail_extra"><div><div class=3D"h5"><br><div class=3D"gmai=
l_quote">On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <span dir=3D"=
ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"_blank">liz@dijkmat.nl=
</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin=
:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>&gt; On 14 N=
ov 2017, at 18:06, Fernando Santagata &lt;<a href=3D"mailto:nando.santagata=
@gmail.com" target=3D"_blank">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; I have several classes, each has some attributes that need to be proce=
ssed in the same way before being passed to other objects.<br>
&gt;<br>
&gt; When I was using Moose, I had some &quot;around&quot; methods that wou=
ld automatically modify the value before delivering it to those attributes,=
 so delegating the object to do the needed adjustments.<br>
&gt;<br>
&gt; Stripped to the bare bones, the thing that in Perl6 looks like this:<b=
r>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0has $!a;<br>
&gt;<br>
&gt;=C2=A0 =C2=A0method a($val?)<br>
&gt;=C2=A0 =C2=A0{<br>
&gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0}<br>
&gt; }<br>
&gt;<br>
&gt; my A $a .=3D new;<br>
&gt; # $a.a =3D 42; # This outputs an error<br>
&gt; $a.a(42);<br>
&gt; say $a.a;<br>
&gt;<br>
&gt; Any hint how to make it work as an assignment, instead of a method cal=
l?<br>
&gt; Better yet, is there a way to abstract that behavior in a role?<br>
<br>
</span>I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $.a is rw;<br>
}<br>
my $obj =3D A.new;<br>
$obj.a =3D 42;<br>
dd $obj;<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
A $obj =3D A.new(a =3D&gt; 42)<br>
<br>
<br>
<br>
Liz</blockquote></div><br><br clear=3D"all"><div><br></div></div></div><spa=
n class=3D"HOEnZb"><font color=3D"#888888">-- <br><div class=3D"m_570182378=
3230936712gmail_signature" data-smartmail=3D"gmail_signature">Fernando Sant=
agata</div>
</font></span></div>
</blockquote></div><br></div>

--94eb2c063e647a9746055df5266d--
0
not
11/14/2017 5:59:17 PM
--001a113d0f12ee0a28055df53909
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi yary,

BUILD and TWEAK work during the object creation time, not at the attribute
assignment time, as far as I know, and I don't have the value to assign to
the attribute at the object creation time yet.

On Tue, Nov 14, 2017 at 6:59 PM, yary <not.com@gmail.com> wrote:

> Fernando, this list recently had a discussion on object creation, and all
> various ways the attributes are checked and set by the various stages. Fr=
om
> that, what you want is TWEAK, or perhaps BUILD, which let you do things
> with the attribute values when the object is created. I leave the researc=
h
> to you (or more verbose, and wiser, list members)...I'm too buuusy!
>
> -y
>
> On Tue, Nov 14, 2017 at 6:51 PM, Fernando Santagata <
> nando.santagata@gmail.com> wrote:
>
>> Hi Liz,
>>
>> What I need is to preprocess the value before assigning it to an
>> attribute.
>>
>> I would do that in Perl5/Moose, using "around", like this:
>>
>> package A;
>> use Moose;
>>
>> has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
>>
>> around [qw(attribute)] =3D> sub {
>>   my ($next, $self, $val) =3D @_;
>>   return $self->$next unless $val;
>>   return $self->$next(preprocess $val); # Preprocess the value before th=
e
>> assignment
>> }
>>
>> In this way I don't have to make an explicit call to the preprocessor an=
y
>> time I assign a value to that attribute, effectively removing that from =
the
>> main program.
>>
>> I'm looking for a way to do that in Perl6.
>>
>> On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
>> wrote:
>>
>>> > On 14 Nov 2017, at 18:06, Fernando Santagata <
>>> nando.santagata@gmail.com> wrote:
>>> > I'm converting a program from Perl5/Moose.
>>> > I have several classes, each has some attributes that need to be
>>> processed in the same way before being passed to other objects.
>>> >
>>> > When I was using Moose, I had some "around" methods that would
>>> automatically modify the value before delivering it to those attributes=
, so
>>> delegating the object to do the needed adjustments.
>>> >
>>> > Stripped to the bare bones, the thing that in Perl6 looks like this:
>>> >
>>> > class A {
>>> >   has $!a;
>>> >
>>> >   method a($val?)
>>> >   {
>>> >     if $val.defined {
>>> >       # Modify $val in some way
>>> >       $!a =3D $val;
>>> >     } else {
>>> >       $!a;
>>> >     }
>>> >   }
>>> > }
>>> >
>>> > my A $a .=3D new;
>>> > # $a.a =3D 42; # This outputs an error
>>> > $a.a(42);
>>> > say $a.a;
>>> >
>>> > Any hint how to make it work as an assignment, instead of a method
>>> call?
>>> > Better yet, is there a way to abstract that behavior in a role?
>>>
>>> I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
>>>
>>> class A {
>>>     has $.a is rw;
>>> }
>>> my $obj =3D A.new;
>>> $obj.a =3D 42;
>>> dd $obj;
>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>>> A $obj =3D A.new(a =3D> 42)
>>>
>>>
>>>
>>> Liz
>>
>>
>>
>>
>> --
>> Fernando Santagata
>>
>
>


--=20
Fernando Santagata

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

<div dir=3D"ltr">Hi yary,<div><br></div><div>BUILD and TWEAK work during th=
e object creation time, not at the attribute assignment time, as far as I k=
now, and I don&#39;t have the value to assign to the attribute at the objec=
t creation time yet.</div></div><div class=3D"gmail_extra"><br><div class=
=3D"gmail_quote">On Tue, Nov 14, 2017 at 6:59 PM, yary <span dir=3D"ltr">&l=
t;<a href=3D"mailto:not.com@gmail.com" target=3D"_blank">not.com@gmail.com<=
/a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:=
0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Fe=
rnando, this list recently had a discussion on object creation, and all var=
ious ways the attributes are checked and set by the various stages. From th=
at, what you want is TWEAK, or perhaps BUILD, which let you do things with =
the attribute values when the object is created. I leave the research to yo=
u (or more verbose, and wiser, list members)...I&#39;m too buuusy!</div><di=
v class=3D"gmail_extra"><span class=3D"HOEnZb"><font color=3D"#888888"><br =
clear=3D"all"><div><div class=3D"m_-9042175660435926629gmail_signature" dat=
a-smartmail=3D"gmail_signature">-y<br></div></div></font></span><div><div c=
lass=3D"h5">
<br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 6:51 PM, Fernando Sa=
ntagata <span dir=3D"ltr">&lt;<a href=3D"mailto:nando.santagata@gmail.com" =
target=3D"_blank">nando.santagata@gmail.com</a>&gt;</span> wrote:<br><block=
quote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc=
 solid;padding-left:1ex"><div dir=3D"ltr">Hi Liz,<div><br></div><div>What I=
 need is to preprocess the value before assigning it to an attribute.</div>=
<div><br></div><div>I would do that in Perl5/Moose, using &quot;around&quot=
;, like this:</div><div><br></div><div>package A;</div><div>use Moose;</div=
><div><br></div><div>has &#39;attribute&#39; =3D&gt;=C2=A0(is =3D&gt; &#39;=
rw&#39;, isa =3D&gt; &#39;Str&#39;);</div><div><br></div><div>around [qw(at=
tribute)] =3D&gt; sub {</div><div><div>=C2=A0 my ($next, $self, $val) =3D @=
_;</div><div>=C2=A0 return $self-&gt;$next unless $val;</div><div>=C2=A0 re=
turn $self-&gt;$next(preprocess $val); # Preprocess the value before the as=
signment</div></div><div>}</div><div><br></div><div>In this way I don&#39;t=
 have to make an explicit call to the preprocessor any time I assign a valu=
e to that attribute, effectively removing that from the main program.</div>=
<div><br></div><div>I&#39;m looking for a way to do that in Perl6.</div></d=
iv><div class=3D"gmail_extra"><div><div class=3D"m_-9042175660435926629h5">=
<br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth M=
attijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"=
_blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex"><span>&gt; On 14 Nov 2017, at 18:06, Fernando Santagata &lt;<a href=3D"=
mailto:nando.santagata@gmail.com" target=3D"_blank">nando.santagata@gmail.c=
om</a>&gt; wrote:<br>
&gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; I have several classes, each has some attributes that need to be proce=
ssed in the same way before being passed to other objects.<br>
&gt;<br>
&gt; When I was using Moose, I had some &quot;around&quot; methods that wou=
ld automatically modify the value before delivering it to those attributes,=
 so delegating the object to do the needed adjustments.<br>
&gt;<br>
&gt; Stripped to the bare bones, the thing that in Perl6 looks like this:<b=
r>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0has $!a;<br>
&gt;<br>
&gt;=C2=A0 =C2=A0method a($val?)<br>
&gt;=C2=A0 =C2=A0{<br>
&gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0}<br>
&gt; }<br>
&gt;<br>
&gt; my A $a .=3D new;<br>
&gt; # $a.a =3D 42; # This outputs an error<br>
&gt; $a.a(42);<br>
&gt; say $a.a;<br>
&gt;<br>
&gt; Any hint how to make it work as an assignment, instead of a method cal=
l?<br>
&gt; Better yet, is there a way to abstract that behavior in a role?<br>
<br>
</span>I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $.a is rw;<br>
}<br>
my $obj =3D A.new;<br>
$obj.a =3D 42;<br>
dd $obj;<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
A $obj =3D A.new(a =3D&gt; 42)<br>
<br>
<br>
<br>
Liz</blockquote></div><br><br clear=3D"all"><div><br></div></div></div><spa=
n class=3D"m_-9042175660435926629HOEnZb"><font color=3D"#888888">-- <br><di=
v class=3D"m_-9042175660435926629m_5701823783230936712gmail_signature" data=
-smartmail=3D"gmail_signature">Fernando Santagata</div>
</font></span></div>
</blockquote></div><br></div></div></div>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata<=
/div>
</div>

--001a113d0f12ee0a28055df53909--
0
nando
11/14/2017 6:05:03 PM
--94eb2c063e64670312055df5429a
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Ah yes, sorry! I'll wait for another answer along with you now.

On Tue, Nov 14, 2017, 7:05 PM Fernando Santagata <nando.santagata@gmail.com=
>
wrote:

> Hi yary,
>
> BUILD and TWEAK work during the object creation time, not at the attribut=
e
> assignment time, as far as I know, and I don't have the value to assign t=
o
> the attribute at the object creation time yet.
>
> On Tue, Nov 14, 2017 at 6:59 PM, yary <not.com@gmail.com> wrote:
>
>> Fernando, this list recently had a discussion on object creation, and al=
l
>> various ways the attributes are checked and set by the various stages. F=
rom
>> that, what you want is TWEAK, or perhaps BUILD, which let you do things
>> with the attribute values when the object is created. I leave the resear=
ch
>> to you (or more verbose, and wiser, list members)...I'm too buuusy!
>>
>> -y
>>
>> On Tue, Nov 14, 2017 at 6:51 PM, Fernando Santagata <
>> nando.santagata@gmail.com> wrote:
>>
>>> Hi Liz,
>>>
>>> What I need is to preprocess the value before assigning it to an
>>> attribute.
>>>
>>> I would do that in Perl5/Moose, using "around", like this:
>>>
>>> package A;
>>> use Moose;
>>>
>>> has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
>>>
>>> around [qw(attribute)] =3D> sub {
>>>   my ($next, $self, $val) =3D @_;
>>>   return $self->$next unless $val;
>>>   return $self->$next(preprocess $val); # Preprocess the value before
>>> the assignment
>>> }
>>>
>>> In this way I don't have to make an explicit call to the preprocessor
>>> any time I assign a value to that attribute, effectively removing that =
from
>>> the main program.
>>>
>>> I'm looking for a way to do that in Perl6.
>>>
>>> On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
>>> wrote:
>>>
>>>> > On 14 Nov 2017, at 18:06, Fernando Santagata <
>>>> nando.santagata@gmail.com> wrote:
>>>> > I'm converting a program from Perl5/Moose.
>>>> > I have several classes, each has some attributes that need to be
>>>> processed in the same way before being passed to other objects.
>>>> >
>>>> > When I was using Moose, I had some "around" methods that would
>>>> automatically modify the value before delivering it to those attribute=
s, so
>>>> delegating the object to do the needed adjustments.
>>>> >
>>>> > Stripped to the bare bones, the thing that in Perl6 looks like this:
>>>> >
>>>> > class A {
>>>> >   has $!a;
>>>> >
>>>> >   method a($val?)
>>>> >   {
>>>> >     if $val.defined {
>>>> >       # Modify $val in some way
>>>> >       $!a =3D $val;
>>>> >     } else {
>>>> >       $!a;
>>>> >     }
>>>> >   }
>>>> > }
>>>> >
>>>> > my A $a .=3D new;
>>>> > # $a.a =3D 42; # This outputs an error
>>>> > $a.a(42);
>>>> > say $a.a;
>>>> >
>>>> > Any hint how to make it work as an assignment, instead of a method
>>>> call?
>>>> > Better yet, is there a way to abstract that behavior in a role?
>>>>
>>>> I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
>>>>
>>>> class A {
>>>>     has $.a is rw;
>>>> }
>>>> my $obj =3D A.new;
>>>> $obj.a =3D 42;
>>>> dd $obj;
>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>>>> A $obj =3D A.new(a =3D> 42)
>>>>
>>>>
>>>>
>>>> Liz
>>>
>>>
>>>
>>>
>>> --
>>> Fernando Santagata
>>>
>>
>>
>
>
> --
> Fernando Santagata
>

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

<p dir=3D"ltr">Ah yes, sorry! I&#39;ll wait for another answer along with y=
ou now.</p>
<br><div class=3D"gmail_quote"><div dir=3D"ltr">On Tue, Nov 14, 2017, 7:05 =
PM Fernando Santagata &lt;<a href=3D"mailto:nando.santagata@gmail.com">nand=
o.santagata@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
"><div dir=3D"ltr">Hi yary,<div><br></div><div>BUILD and TWEAK work during =
the object creation time, not at the attribute assignment time, as far as I=
 know, and I don&#39;t have the value to assign to the attribute at the obj=
ect creation time yet.</div></div><div class=3D"gmail_extra"></div><div cla=
ss=3D"gmail_extra"><br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 6=
:59 PM, yary <span dir=3D"ltr">&lt;<a href=3D"mailto:not.com@gmail.com" tar=
get=3D"_blank">not.com@gmail.com</a>&gt;</span> wrote:<br><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr">Fernando, this list recently had a discussio=
n on object creation, and all various ways the attributes are checked and s=
et by the various stages. From that, what you want is TWEAK, or perhaps BUI=
LD, which let you do things with the attribute values when the object is cr=
eated. I leave the research to you (or more verbose, and wiser, list member=
s)...I&#39;m too buuusy!</div><div class=3D"gmail_extra"><span class=3D"m_-=
7839833343785726215HOEnZb"><font color=3D"#888888"><br clear=3D"all"><div><=
div class=3D"m_-7839833343785726215m_-9042175660435926629gmail_signature" d=
ata-smartmail=3D"gmail_signature">-y<br></div></div></font></span><div><div=
 class=3D"m_-7839833343785726215h5">
<br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 6:51 PM, Fernando Sa=
ntagata <span dir=3D"ltr">&lt;<a href=3D"mailto:nando.santagata@gmail.com" =
target=3D"_blank">nando.santagata@gmail.com</a>&gt;</span> wrote:<br><block=
quote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc=
 solid;padding-left:1ex"><div dir=3D"ltr">Hi Liz,<div><br></div><div>What I=
 need is to preprocess the value before assigning it to an attribute.</div>=
<div><br></div><div>I would do that in Perl5/Moose, using &quot;around&quot=
;, like this:</div><div><br></div><div>package A;</div><div>use Moose;</div=
><div><br></div><div>has &#39;attribute&#39; =3D&gt;=C2=A0(is =3D&gt; &#39;=
rw&#39;, isa =3D&gt; &#39;Str&#39;);</div><div><br></div><div>around [qw(at=
tribute)] =3D&gt; sub {</div><div><div>=C2=A0 my ($next, $self, $val) =3D @=
_;</div><div>=C2=A0 return $self-&gt;$next unless $val;</div><div>=C2=A0 re=
turn $self-&gt;$next(preprocess $val); # Preprocess the value before the as=
signment</div></div><div>}</div><div><br></div><div>In this way I don&#39;t=
 have to make an explicit call to the preprocessor any time I assign a valu=
e to that attribute, effectively removing that from the main program.</div>=
<div><br></div><div>I&#39;m looking for a way to do that in Perl6.</div></d=
iv><div class=3D"gmail_extra"><div><div class=3D"m_-7839833343785726215m_-9=
042175660435926629h5"><br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 a=
t 6:11 PM, Elizabeth Mattijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@=
dijkmat.nl" target=3D"_blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><bloc=
kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc=
c solid;padding-left:1ex"><span>&gt; On 14 Nov 2017, at 18:06, Fernando San=
tagata &lt;<a href=3D"mailto:nando.santagata@gmail.com" target=3D"_blank">n=
ando.santagata@gmail.com</a>&gt; wrote:<br>
&gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; I have several classes, each has some attributes that need to be proce=
ssed in the same way before being passed to other objects.<br>
&gt;<br>
&gt; When I was using Moose, I had some &quot;around&quot; methods that wou=
ld automatically modify the value before delivering it to those attributes,=
 so delegating the object to do the needed adjustments.<br>
&gt;<br>
&gt; Stripped to the bare bones, the thing that in Perl6 looks like this:<b=
r>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0has $!a;<br>
&gt;<br>
&gt;=C2=A0 =C2=A0method a($val?)<br>
&gt;=C2=A0 =C2=A0{<br>
&gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0}<br>
&gt; }<br>
&gt;<br>
&gt; my A $a .=3D new;<br>
&gt; # $a.a =3D 42; # This outputs an error<br>
&gt; $a.a(42);<br>
&gt; say $a.a;<br>
&gt;<br>
&gt; Any hint how to make it work as an assignment, instead of a method cal=
l?<br>
&gt; Better yet, is there a way to abstract that behavior in a role?<br>
<br>
</span>I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $.a is rw;<br>
}<br>
my $obj =3D A.new;<br>
$obj.a =3D 42;<br>
dd $obj;<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
A $obj =3D A.new(a =3D&gt; 42)<br>
<br>
<br>
<br>
Liz</blockquote></div><br><br clear=3D"all"><div><br></div></div></div><spa=
n class=3D"m_-7839833343785726215m_-9042175660435926629HOEnZb"><font color=
=3D"#888888">-- <br><div class=3D"m_-7839833343785726215m_-9042175660435926=
629m_5701823783230936712gmail_signature" data-smartmail=3D"gmail_signature"=
>Fernando Santagata</div>
</font></span></div>
</blockquote></div><br></div></div></div>
</blockquote></div><br><br clear=3D"all"><div><br></div></div><div class=3D=
"gmail_extra">-- <br><div class=3D"m_-7839833343785726215gmail_signature" d=
ata-smartmail=3D"gmail_signature">Fernando Santagata</div>
</div></blockquote></div>

--94eb2c063e64670312055df5429a--
0
not
11/14/2017 6:07:15 PM
This might it then:

class A {
    has $!a;  # $.a if you want to be able to assign with .new
    method a() {
        Proxy.new(
          FETCH =3D> { $!a },
          STORE =3D> -> $, $value { $!a =3D $value * 2 }
        )
    }
}
my $a =3D A.new;
$a.a =3D 77;
dd $a.a;   # 154

> On 14 Nov 2017, at 18:51, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
>=20
> Hi Liz,
>=20
> What I need is to preprocess the value before assigning it to an =
attribute.
>=20
> I would do that in Perl5/Moose, using "around", like this:
>=20
> package A;
> use Moose;
>=20
> has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
>=20
> around [qw(attribute)] =3D> sub {
>   my ($next, $self, $val) =3D @_;
>   return $self->$next unless $val;
>   return $self->$next(preprocess $val); # Preprocess the value before =
the assignment
> }
>=20
> In this way I don't have to make an explicit call to the preprocessor =
any time I assign a value to that attribute, effectively removing that =
from the main program.
>=20
> I'm looking for a way to do that in Perl6.
>=20
> On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl> =
wrote:
> > On 14 Nov 2017, at 18:06, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> > I'm converting a program from Perl5/Moose.
> > I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.
> >
> > When I was using Moose, I had some "around" methods that would =
automatically modify the value before delivering it to those attributes, =
so delegating the object to do the needed adjustments.
> >
> > Stripped to the bare bones, the thing that in Perl6 looks like this:
> >
> > class A {
> >   has $!a;
> >
> >   method a($val?)
> >   {
> >     if $val.defined {
> >       # Modify $val in some way
> >       $!a =3D $val;
> >     } else {
> >       $!a;
> >     }
> >   }
> > }
> >
> > my A $a .=3D new;
> > # $a.a =3D 42; # This outputs an error
> > $a.a(42);
> > say $a.a;
> >
> > Any hint how to make it work as an assignment, instead of a method =
call?
> > Better yet, is there a way to abstract that behavior in a role?
>=20
> I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
>=20
> class A {
>     has $.a is rw;
> }
> my $obj =3D A.new;
> $obj.a =3D 42;
> dd $obj;
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> A $obj =3D A.new(a =3D> 42)
>=20
>=20
>=20
> Liz
>=20
>=20
>=20
> --=20
> Fernando Santagata
0
liz
11/14/2017 6:09:08 PM
--001a113d21d42aca4d055df56dfd
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thank you, Liz!

On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth Mattijsen <liz@dijkmat.nl> wrote=
:

> This might it then:
>
> class A {
>     has $!a;  # $.a if you want to be able to assign with .new
>     method a() {
>         Proxy.new(
>           FETCH =3D> { $!a },
>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>         )
>     }
> }
> my $a =3D A.new;
> $a.a =3D 77;
> dd $a.a;   # 154
>
> > On 14 Nov 2017, at 18:51, Fernando Santagata <nando.santagata@gmail.com=
>
> wrote:
> >
> > Hi Liz,
> >
> > What I need is to preprocess the value before assigning it to an
> attribute.
> >
> > I would do that in Perl5/Moose, using "around", like this:
> >
> > package A;
> > use Moose;
> >
> > has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
> >
> > around [qw(attribute)] =3D> sub {
> >   my ($next, $self, $val) =3D @_;
> >   return $self->$next unless $val;
> >   return $self->$next(preprocess $val); # Preprocess the value before
> the assignment
> > }
> >
> > In this way I don't have to make an explicit call to the preprocessor
> any time I assign a value to that attribute, effectively removing that fr=
om
> the main program.
> >
> > I'm looking for a way to do that in Perl6.
> >
> > On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
> wrote:
> > > On 14 Nov 2017, at 18:06, Fernando Santagata <
> nando.santagata@gmail.com> wrote:
> > > I'm converting a program from Perl5/Moose.
> > > I have several classes, each has some attributes that need to be
> processed in the same way before being passed to other objects.
> > >
> > > When I was using Moose, I had some "around" methods that would
> automatically modify the value before delivering it to those attributes, =
so
> delegating the object to do the needed adjustments.
> > >
> > > Stripped to the bare bones, the thing that in Perl6 looks like this:
> > >
> > > class A {
> > >   has $!a;
> > >
> > >   method a($val?)
> > >   {
> > >     if $val.defined {
> > >       # Modify $val in some way
> > >       $!a =3D $val;
> > >     } else {
> > >       $!a;
> > >     }
> > >   }
> > > }
> > >
> > > my A $a .=3D new;
> > > # $a.a =3D 42; # This outputs an error
> > > $a.a(42);
> > > say $a.a;
> > >
> > > Any hint how to make it work as an assignment, instead of a method
> call?
> > > Better yet, is there a way to abstract that behavior in a role?
> >
> > I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
> >
> > class A {
> >     has $.a is rw;
> > }
> > my $obj =3D A.new;
> > $obj.a =3D 42;
> > dd $obj;
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > A $obj =3D A.new(a =3D> 42)
> >
> >
> >
> > Liz
> >
> >
> >
> > --
> > Fernando Santagata
>



--=20
Fernando Santagata

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

<div dir=3D"ltr">Thank you, Liz!</div><div class=3D"gmail_extra"><br><div c=
lass=3D"gmail_quote">On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth Mattijsen <=
span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"_blank">li=
z@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This m=
ight it then:<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $!a;=C2=A0 # $.a if you want to be able to assign with .n=
ew<br>
=C2=A0 =C2=A0 method a() {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D =
$value * 2 }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
=C2=A0 =C2=A0 }<br>
}<br>
my $a =3D A.new;<br>
$a.a =3D 77;<br>
dd $a.a;=C2=A0 =C2=A0# 154<br>
<div class=3D"HOEnZb"><div class=3D"h5"><br>
&gt; On 14 Nov 2017, at 18:51, Fernando Santagata &lt;<a href=3D"mailto:nan=
do.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi Liz,<br>
&gt;<br>
&gt; What I need is to preprocess the value before assigning it to an attri=
bute.<br>
&gt;<br>
&gt; I would do that in Perl5/Moose, using &quot;around&quot;, like this:<b=
r>
&gt;<br>
&gt; package A;<br>
&gt; use Moose;<br>
&gt;<br>
&gt; has &#39;attribute&#39; =3D&gt; (is =3D&gt; &#39;rw&#39;, isa =3D&gt; =
&#39;Str&#39;);<br>
&gt;<br>
&gt; around [qw(attribute)] =3D&gt; sub {<br>
&gt;=C2=A0 =C2=A0my ($next, $self, $val) =3D @_;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next unless $val;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next(preprocess $val); # Preprocess the =
value before the assignment<br>
&gt; }<br>
&gt;<br>
&gt; In this way I don&#39;t have to make an explicit call to the preproces=
sor any time I assign a value to that attribute, effectively removing that =
from the main program.<br>
&gt;<br>
&gt; I&#39;m looking for a way to do that in Perl6.<br>
&gt;<br>
&gt; On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen &lt;<a href=3D"ma=
ilto:liz@dijkmat.nl">liz@dijkmat.nl</a>&gt; wrote:<br>
&gt; &gt; On 14 Nov 2017, at 18:06, Fernando Santagata &lt;<a href=3D"mailt=
o:nando.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt; &gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; &gt; I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.<br>
&gt; &gt;<br>
&gt; &gt; When I was using Moose, I had some &quot;around&quot; methods tha=
t would automatically modify the value before delivering it to those attrib=
utes, so delegating the object to do the needed adjustments.<br>
&gt; &gt;<br>
&gt; &gt; Stripped to the bare bones, the thing that in Perl6 looks like th=
is:<br>
&gt; &gt;<br>
&gt; &gt; class A {<br>
&gt; &gt;=C2=A0 =C2=A0has $!a;<br>
&gt; &gt;<br>
&gt; &gt;=C2=A0 =C2=A0method a($val?)<br>
&gt; &gt;=C2=A0 =C2=A0{<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt; &gt;=C2=A0 =C2=A0}<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; my A $a .=3D new;<br>
&gt; &gt; # $a.a =3D 42; # This outputs an error<br>
&gt; &gt; $a.a(42);<br>
&gt; &gt; say $a.a;<br>
&gt; &gt;<br>
&gt; &gt; Any hint how to make it work as an assignment, instead of a metho=
d call?<br>
&gt; &gt; Better yet, is there a way to abstract that behavior in a role?<b=
r>
&gt;<br>
&gt; I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0 =C2=A0has $.a is rw;<br>
&gt; }<br>
&gt; my $obj =3D A.new;<br>
&gt; $obj.a =3D 42;<br>
&gt; dd $obj;<br>
&gt; =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
&gt; A $obj =3D A.new(a =3D&gt; 42)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Liz<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Fernando Santagata<br>
</div></div></blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>=
<div class=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando =
Santagata</div>
</div>

--001a113d21d42aca4d055df56dfd--
0
nando
11/14/2017 6:19:22 PM
--001a1143dcfea84793055df5b3f7
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Neat! Just read the Proxy class doc at https://docs.perl6.org/type/Proxy

Instead of hooking into any accessor magic, this solution creates a Proxy
object that lives inside the "a" method. Every time you use the "a" method,
it generates a new Proxy object, which will either set or retrieve the $!a
attribute from the A object.

Two minor quibbles, and since I'm not the author or user, take them with a
grain of salt:

 " $.a if you want to be able to assign with .new "  - in that case, $!a
won't go through the modifications.


class A {
    has $.a;
    method a() {
        Proxy.new(
          FETCH =3D> { $!a },
          STORE =3D> -> $, $value { $!a =3D $value * 2 }
        )
    }
}
my $a =3D A.new(a =3D> 23);
dd $a.a;   # 23

$a.a =3D 34;
dd $a.a;   # 68

A way to fix that, is to have the STORE function be a named routine that
both the proxy object & a TWEAK submethod can use, as each sets $!a.


The other quibble, is that it creates and discards the proxy object with
each call. (Not at all a problem functionally and likely not anything that
would affect performance either.) Here's a demo

class A {
    has $!a;
    method a() {
    say "baking a new Proxy object for class A";
        Proxy.new(
          FETCH =3D> { $!a },
          STORE =3D> -> $, $value { $!a =3D $value * 2 }
        )
    }
}

my $a =3D A.new(); # no output
$a.a =3D 34; # "baking..."
dd $a.a;   # "baking...", "68"



-y

On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth Mattijsen <liz@dijkmat.nl> wrote=
:

> This might it then:
>
> class A {
>     has $!a;  # $.a if you want to be able to assign with .new
>     method a() {
>         Proxy.new(
>           FETCH =3D> { $!a },
>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>         )
>     }
> }
> my $a =3D A.new;
> $a.a =3D 77;
> dd $a.a;   # 154
>
> > On 14 Nov 2017, at 18:51, Fernando Santagata <nando.santagata@gmail.com=
>
> wrote:
> >
> > Hi Liz,
> >
> > What I need is to preprocess the value before assigning it to an
> attribute.
> >
> > I would do that in Perl5/Moose, using "around", like this:
> >
> > package A;
> > use Moose;
> >
> > has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
> >
> > around [qw(attribute)] =3D> sub {
> >   my ($next, $self, $val) =3D @_;
> >   return $self->$next unless $val;
> >   return $self->$next(preprocess $val); # Preprocess the value before
> the assignment
> > }
> >
> > In this way I don't have to make an explicit call to the preprocessor
> any time I assign a value to that attribute, effectively removing that fr=
om
> the main program.
> >
> > I'm looking for a way to do that in Perl6.
> >
> > On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
> wrote:
> > > On 14 Nov 2017, at 18:06, Fernando Santagata <
> nando.santagata@gmail.com> wrote:
> > > I'm converting a program from Perl5/Moose.
> > > I have several classes, each has some attributes that need to be
> processed in the same way before being passed to other objects.
> > >
> > > When I was using Moose, I had some "around" methods that would
> automatically modify the value before delivering it to those attributes, =
so
> delegating the object to do the needed adjustments.
> > >
> > > Stripped to the bare bones, the thing that in Perl6 looks like this:
> > >
> > > class A {
> > >   has $!a;
> > >
> > >   method a($val?)
> > >   {
> > >     if $val.defined {
> > >       # Modify $val in some way
> > >       $!a =3D $val;
> > >     } else {
> > >       $!a;
> > >     }
> > >   }
> > > }
> > >
> > > my A $a .=3D new;
> > > # $a.a =3D 42; # This outputs an error
> > > $a.a(42);
> > > say $a.a;
> > >
> > > Any hint how to make it work as an assignment, instead of a method
> call?
> > > Better yet, is there a way to abstract that behavior in a role?
> >
> > I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
> >
> > class A {
> >     has $.a is rw;
> > }
> > my $obj =3D A.new;
> > $obj.a =3D 42;
> > dd $obj;
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > A $obj =3D A.new(a =3D> 42)
> >
> >
> >
> > Liz
> >
> >
> >
> > --
> > Fernando Santagata
>

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

<div dir=3D"ltr">Neat! Just read the Proxy class doc at=C2=A0<a href=3D"htt=
ps://docs.perl6.org/type/Proxy">https://docs.perl6.org/type/Proxy</a><div><=
br></div><div>Instead of hooking into any accessor magic, this solution cre=
ates a Proxy object that lives inside the &quot;a&quot; method. Every time =
you use the &quot;a&quot; method, it generates a new Proxy object, which wi=
ll either set or retrieve the $!a attribute from the A object.</div><div><b=
r></div><div>Two minor quibbles, and since I&#39;m not the author or user, =
take them with a grain of salt:</div><div><br></div><div>=C2=A0&quot;=C2=A0=
<span style=3D"font-size:12.8px">$.a if you want to be able to assign with =
..new &quot;=C2=A0 - in that case, $!a won&#39;t go through the modification=
s.</span></div><div><span style=3D"font-size:12.8px"><br></span></div><div>=
<span style=3D"font-size:12.8px"><br></span></div><div><div><span style=3D"=
font-size:12.8px">class A {</span></div><div><span style=3D"font-size:12.8p=
x">=C2=A0 =C2=A0 has $.a;</span></div><div><span style=3D"font-size:12.8px"=
>=C2=A0 =C2=A0 method a() {</span></div><div><span style=3D"font-size:12.8p=
x">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(</span></div><div><span style=3D"f=
ont-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },<=
/span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D $value * 2 }</span></div=
><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 )</span>=
</div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 }</span></div><di=
v><span style=3D"font-size:12.8px">}</span></div><div><span style=3D"font-s=
ize:12.8px">my $a =3D A.new(a =3D&gt; 23);</span></div><div><span style=3D"=
font-size:12.8px">dd $a.a;=C2=A0 =C2=A0# 23=C2=A0</span></div><div><span st=
yle=3D"font-size:12.8px"><br></span></div><div><span style=3D"font-size:12.=
8px">$a.a =3D 34;</span></div><div><span style=3D"font-size:12.8px">dd $a.a=
;=C2=A0 =C2=A0# 68</span></div></div><div><span style=3D"font-size:12.8px">=
<br></span></div><div><span style=3D"font-size:12.8px">A way to fix that, i=
s to have the STORE function be a named routine that both the proxy object =
&amp; a TWEAK submethod can use, as each sets $!a.</span><span style=3D"fon=
t-size:12.8px"><br></span></div><div><span style=3D"font-size:12.8px"><br><=
/span></div><div><br></div><div><span style=3D"font-size:12.8px">The other =
quibble, is that it creates and discards the proxy object with each call. (=
Not at all a problem functionally and likely not anything that would affect=
 performance either.) Here&#39;s a demo</span></div><div><span style=3D"fon=
t-size:12.8px"><br></span></div><div><div><span style=3D"font-size:12.8px">=
class A {</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 h=
as $!a;</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 met=
hod a() {</span></div><div><span style=3D"font-size:12.8px"><span style=3D"=
white-space:pre">	</span>=C2=A0 =C2=A0 say &quot;baking a new Proxy object =
for class A&quot;;</span></div><div><span style=3D"font-size:12.8px">=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 Proxy.new(</span></div><div><span style=3D"font-size:=
12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },</span></d=
iv><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 STORE =3D&gt; -&gt; $, $value { $!a =3D $value * 2 }</span></div><div><spa=
n style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 )</span></div><div=
><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 }</span></div><div><span st=
yle=3D"font-size:12.8px">}</span></div><div><span style=3D"font-size:12.8px=
"><br></span></div><div><span style=3D"font-size:12.8px">my $a =3D A.new();=
 # no output</span></div><div><span style=3D"font-size:12.8px">$a.a =3D 34;=
 # &quot;baking...&quot;</span></div><div><span style=3D"font-size:12.8px">=
dd $a.a;=C2=A0 =C2=A0# &quot;baking...&quot;, &quot;68&quot;</span></div><d=
iv style=3D"font-size:12.8px"><br></div></div><div><span style=3D"font-size=
:12.8px"><br></span></div></div><div class=3D"gmail_extra"><br clear=3D"all=
"><div><div class=3D"gmail_signature" data-smartmail=3D"gmail_signature">-y=
<br></div></div>
<br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth M=
attijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"=
_blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex">This might it then:<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $!a;=C2=A0 # $.a if you want to be able to assign with .n=
ew<br>
=C2=A0 =C2=A0 method a() {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D =
$value * 2 }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
=C2=A0 =C2=A0 }<br>
}<br>
my $a =3D A.new;<br>
$a.a =3D 77;<br>
dd $a.a;=C2=A0 =C2=A0# 154<br>
<div class=3D"HOEnZb"><div class=3D"h5"><br>
&gt; On 14 Nov 2017, at 18:51, Fernando Santagata &lt;<a href=3D"mailto:nan=
do.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi Liz,<br>
&gt;<br>
&gt; What I need is to preprocess the value before assigning it to an attri=
bute.<br>
&gt;<br>
&gt; I would do that in Perl5/Moose, using &quot;around&quot;, like this:<b=
r>
&gt;<br>
&gt; package A;<br>
&gt; use Moose;<br>
&gt;<br>
&gt; has &#39;attribute&#39; =3D&gt; (is =3D&gt; &#39;rw&#39;, isa =3D&gt; =
&#39;Str&#39;);<br>
&gt;<br>
&gt; around [qw(attribute)] =3D&gt; sub {<br>
&gt;=C2=A0 =C2=A0my ($next, $self, $val) =3D @_;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next unless $val;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next(preprocess $val); # Preprocess the =
value before the assignment<br>
&gt; }<br>
&gt;<br>
&gt; In this way I don&#39;t have to make an explicit call to the preproces=
sor any time I assign a value to that attribute, effectively removing that =
from the main program.<br>
&gt;<br>
&gt; I&#39;m looking for a way to do that in Perl6.<br>
&gt;<br>
&gt; On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen &lt;<a href=3D"ma=
ilto:liz@dijkmat.nl">liz@dijkmat.nl</a>&gt; wrote:<br>
&gt; &gt; On 14 Nov 2017, at 18:06, Fernando Santagata &lt;<a href=3D"mailt=
o:nando.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt; &gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; &gt; I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.<br>
&gt; &gt;<br>
&gt; &gt; When I was using Moose, I had some &quot;around&quot; methods tha=
t would automatically modify the value before delivering it to those attrib=
utes, so delegating the object to do the needed adjustments.<br>
&gt; &gt;<br>
&gt; &gt; Stripped to the bare bones, the thing that in Perl6 looks like th=
is:<br>
&gt; &gt;<br>
&gt; &gt; class A {<br>
&gt; &gt;=C2=A0 =C2=A0has $!a;<br>
&gt; &gt;<br>
&gt; &gt;=C2=A0 =C2=A0method a($val?)<br>
&gt; &gt;=C2=A0 =C2=A0{<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt; &gt;=C2=A0 =C2=A0}<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; my A $a .=3D new;<br>
&gt; &gt; # $a.a =3D 42; # This outputs an error<br>
&gt; &gt; $a.a(42);<br>
&gt; &gt; say $a.a;<br>
&gt; &gt;<br>
&gt; &gt; Any hint how to make it work as an assignment, instead of a metho=
d call?<br>
&gt; &gt; Better yet, is there a way to abstract that behavior in a role?<b=
r>
&gt;<br>
&gt; I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0 =C2=A0has $.a is rw;<br>
&gt; }<br>
&gt; my $obj =3D A.new;<br>
&gt; $obj.a =3D 42;<br>
&gt; dd $obj;<br>
&gt; =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
&gt; A $obj =3D A.new(a =3D&gt; 42)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Liz<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Fernando Santagata<br>
</div></div></blockquote></div><br></div>

--001a1143dcfea84793055df5b3f7--
0
not
11/14/2017 6:38:45 PM
--94eb2c13056a711595055df612f3
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Interesting!

I'm trying to avoid to overbake :) the Proxy, by assigning it to a 'state'
variable or creating a method on-the-fly, in the hope it's treated as a
closure, but no luck so far:

class A {
  has $.a;
  submethod BUILD {
    A.^add_method('proxy', my method proxy(A:) {
      say 'Creating a proxy method...';
      Proxy.new(
        FETCH =3D> { $!a },
        STORE =3D> -> $, $value { $!a =3D $value * 2 }
      )
    });
  }
  method a()
  {
    self.proxy;
  }
}

my A $a .=3D new;
$a.a =3D 21;
say $a.a;

Output:

Creating a proxy method...
Creating a proxy method...
42


On Tue, Nov 14, 2017 at 7:38 PM, yary <not.com@gmail.com> wrote:

> Neat! Just read the Proxy class doc at https://docs.perl6.org/type/Proxy
>
> Instead of hooking into any accessor magic, this solution creates a Proxy
> object that lives inside the "a" method. Every time you use the "a" metho=
d,
> it generates a new Proxy object, which will either set or retrieve the $!=
a
> attribute from the A object.
>
> Two minor quibbles, and since I'm not the author or user, take them with =
a
> grain of salt:
>
>  " $.a if you want to be able to assign with .new "  - in that case, $!a
> won't go through the modifications.
>
>
> class A {
>     has $.a;
>     method a() {
>         Proxy.new(
>           FETCH =3D> { $!a },
>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>         )
>     }
> }
> my $a =3D A.new(a =3D> 23);
> dd $a.a;   # 23
>
> $a.a =3D 34;
> dd $a.a;   # 68
>
> A way to fix that, is to have the STORE function be a named routine that
> both the proxy object & a TWEAK submethod can use, as each sets $!a.
>
>
> The other quibble, is that it creates and discards the proxy object with
> each call. (Not at all a problem functionally and likely not anything tha=
t
> would affect performance either.) Here's a demo
>
> class A {
>     has $!a;
>     method a() {
>     say "baking a new Proxy object for class A";
>         Proxy.new(
>           FETCH =3D> { $!a },
>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>         )
>     }
> }
>
> my $a =3D A.new(); # no output
> $a.a =3D 34; # "baking..."
> dd $a.a;   # "baking...", "68"
>
>
>
> -y
>
> On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
> wrote:
>
>> This might it then:
>>
>> class A {
>>     has $!a;  # $.a if you want to be able to assign with .new
>>     method a() {
>>         Proxy.new(
>>           FETCH =3D> { $!a },
>>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>>         )
>>     }
>> }
>> my $a =3D A.new;
>> $a.a =3D 77;
>> dd $a.a;   # 154
>>
>> > On 14 Nov 2017, at 18:51, Fernando Santagata <nando.santagata@gmail.co=
m>
>> wrote:
>> >
>> > Hi Liz,
>> >
>> > What I need is to preprocess the value before assigning it to an
>> attribute.
>> >
>> > I would do that in Perl5/Moose, using "around", like this:
>> >
>> > package A;
>> > use Moose;
>> >
>> > has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
>> >
>> > around [qw(attribute)] =3D> sub {
>> >   my ($next, $self, $val) =3D @_;
>> >   return $self->$next unless $val;
>> >   return $self->$next(preprocess $val); # Preprocess the value before
>> the assignment
>> > }
>> >
>> > In this way I don't have to make an explicit call to the preprocessor
>> any time I assign a value to that attribute, effectively removing that f=
rom
>> the main program.
>> >
>> > I'm looking for a way to do that in Perl6.
>> >
>> > On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
>> wrote:
>> > > On 14 Nov 2017, at 18:06, Fernando Santagata <
>> nando.santagata@gmail.com> wrote:
>> > > I'm converting a program from Perl5/Moose.
>> > > I have several classes, each has some attributes that need to be
>> processed in the same way before being passed to other objects.
>> > >
>> > > When I was using Moose, I had some "around" methods that would
>> automatically modify the value before delivering it to those attributes,=
 so
>> delegating the object to do the needed adjustments.
>> > >
>> > > Stripped to the bare bones, the thing that in Perl6 looks like this:
>> > >
>> > > class A {
>> > >   has $!a;
>> > >
>> > >   method a($val?)
>> > >   {
>> > >     if $val.defined {
>> > >       # Modify $val in some way
>> > >       $!a =3D $val;
>> > >     } else {
>> > >       $!a;
>> > >     }
>> > >   }
>> > > }
>> > >
>> > > my A $a .=3D new;
>> > > # $a.a =3D 42; # This outputs an error
>> > > $a.a(42);
>> > > say $a.a;
>> > >
>> > > Any hint how to make it work as an assignment, instead of a method
>> call?
>> > > Better yet, is there a way to abstract that behavior in a role?
>> >
>> > I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
>> >
>> > class A {
>> >     has $.a is rw;
>> > }
>> > my $obj =3D A.new;
>> > $obj.a =3D 42;
>> > dd $obj;
>> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>> > A $obj =3D A.new(a =3D> 42)
>> >
>> >
>> >
>> > Liz
>> >
>> >
>> >
>> > --
>> > Fernando Santagata
>>
>
>


--=20
Fernando Santagata

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

<div dir=3D"ltr">Interesting!<div><br></div><div>I&#39;m trying to avoid to=
 overbake :) the Proxy, by assigning it to a &#39;state&#39; variable or cr=
eating a method on-the-fly, in the hope it&#39;s treated as a closure, but =
no luck so far:</div><div><br></div><div><div>class A {</div><div>=C2=A0 ha=
s $.a;</div><div>=C2=A0 submethod BUILD {</div><div>=C2=A0 =C2=A0 A.^add_me=
thod(&#39;proxy&#39;, my method proxy(A:) {</div><div>=C2=A0 =C2=A0 =C2=A0 =
say &#39;Creating a proxy method...&#39;;</div><div>=C2=A0 =C2=A0 =C2=A0 Pr=
oxy.new(</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },</div>=
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D $v=
alue * 2 }</div><div>=C2=A0 =C2=A0 =C2=A0 )</div><div>=C2=A0 =C2=A0 });</di=
v><div>=C2=A0 }</div><div>=C2=A0 method a()</div><div>=C2=A0 {</div><div>=
=C2=A0 =C2=A0 self.proxy;</div><div>=C2=A0 }</div><div>}</div><div><br></di=
v><div>my A $a .=3D new;</div><div>$a.a =3D 21;</div><div>say $a.a;</div></=
div><div><br></div><div>Output:</div><div><br></div><div>Creating a proxy m=
ethod...<div>Creating a proxy method...</div><div>42</div><br></div></div><=
div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Tue, Nov 14, 20=
17 at 7:38 PM, yary <span dir=3D"ltr">&lt;<a href=3D"mailto:not.com@gmail.c=
om" target=3D"_blank">not.com@gmail.com</a>&gt;</span> wrote:<br><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><div dir=3D"ltr">Neat! Just read the Proxy class doc a=
t=C2=A0<a href=3D"https://docs.perl6.org/type/Proxy" target=3D"_blank">http=
s://docs.perl6.org/<wbr>type/Proxy</a><div><br></div><div>Instead of hookin=
g into any accessor magic, this solution creates a Proxy object that lives =
inside the &quot;a&quot; method. Every time you use the &quot;a&quot; metho=
d, it generates a new Proxy object, which will either set or retrieve the $=
!a attribute from the A object.</div><div><br></div><div>Two minor quibbles=
, and since I&#39;m not the author or user, take them with a grain of salt:=
</div><div><br></div><div>=C2=A0&quot;=C2=A0<span style=3D"font-size:12.8px=
">$.a if you want to be able to assign with .new &quot;=C2=A0 - in that cas=
e, $!a won&#39;t go through the modifications.</span></div><div><span style=
=3D"font-size:12.8px"><br></span></div><div><span style=3D"font-size:12.8px=
"><br></span></div><div><div><span style=3D"font-size:12.8px">class A {</sp=
an></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 has $.a;</span=
></div><span class=3D""><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0=
 method a() {</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 Proxy.new(</span></div><div><span style=3D"font-size:12.8=
px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },</span></div><=
div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STO=
RE =3D&gt; -&gt; $, $value { $!a =3D $value * 2 }</span></div><div><span st=
yle=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 )</span></div><div><sp=
an style=3D"font-size:12.8px">=C2=A0 =C2=A0 }</span></div><div><span style=
=3D"font-size:12.8px">}</span></div></span><div><span style=3D"font-size:12=
..8px">my $a =3D A.new(a =3D&gt; 23);</span></div><div><span style=3D"font-s=
ize:12.8px">dd $a.a;=C2=A0 =C2=A0# 23=C2=A0</span></div><div><span style=3D=
"font-size:12.8px"><br></span></div><div><span style=3D"font-size:12.8px">$=
a.a =3D 34;</span></div><div><span style=3D"font-size:12.8px">dd $a.a;=C2=
=A0 =C2=A0# 68</span></div></div><div><span style=3D"font-size:12.8px"><br>=
</span></div><div><span style=3D"font-size:12.8px">A way to fix that, is to=
 have the STORE function be a named routine that both the proxy object &amp=
; a TWEAK submethod can use, as each sets $!a.</span><span style=3D"font-si=
ze:12.8px"><br></span></div><div><span style=3D"font-size:12.8px"><br></spa=
n></div><div><br></div><div><span style=3D"font-size:12.8px">The other quib=
ble, is that it creates and discards the proxy object with each call. (Not =
at all a problem functionally and likely not anything that would affect per=
formance either.) Here&#39;s a demo</span></div><div><span style=3D"font-si=
ze:12.8px"><br></span></div><div><div><span style=3D"font-size:12.8px">clas=
s A {</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 has $=
!a;</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 method =
a() {</span></div><div><span style=3D"font-size:12.8px"><span style=3D"whit=
e-space:pre-wrap">	</span>=C2=A0 =C2=A0 say &quot;baking a new Proxy object=
 for class A&quot;;</span></div><span class=3D""><div><span style=3D"font-s=
ize:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(</span></div><div><span s=
tyle=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; =
{ $!a },</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D $value * 2 }</=
span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 )</span></div><div><span style=3D"font-size:12.8px">=C2=A0 =C2=A0 }</sp=
an></div><div><span style=3D"font-size:12.8px">}</span></div><div><span sty=
le=3D"font-size:12.8px"><br></span></div></span><div><span style=3D"font-si=
ze:12.8px">my $a =3D A.new(); # no output</span></div><div><span style=3D"f=
ont-size:12.8px">$a.a =3D 34; # &quot;baking...&quot;</span></div><div><spa=
n style=3D"font-size:12.8px">dd $a.a;=C2=A0 =C2=A0# &quot;baking...&quot;, =
&quot;68&quot;</span></div><span class=3D"HOEnZb"><font color=3D"#888888"><=
div style=3D"font-size:12.8px"><br></div></font></span></div><span class=3D=
"HOEnZb"><font color=3D"#888888"><div><span style=3D"font-size:12.8px"><br>=
</span></div></font></span></div><div class=3D"gmail_extra"><span class=3D"=
HOEnZb"><font color=3D"#888888"><br clear=3D"all"><div><div class=3D"m_-645=
2164812230221016gmail_signature" data-smartmail=3D"gmail_signature">-y<br><=
/div></div></font></span><div><div class=3D"h5">
<br><div class=3D"gmail_quote">On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth M=
attijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"=
_blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex">This might it then:<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $!a;=C2=A0 # $.a if you want to be able to assign with .n=
ew<br>
=C2=A0 =C2=A0 method a() {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D =
$value * 2 }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
=C2=A0 =C2=A0 }<br>
}<br>
my $a =3D A.new;<br>
$a.a =3D 77;<br>
dd $a.a;=C2=A0 =C2=A0# 154<br>
<div class=3D"m_-6452164812230221016HOEnZb"><div class=3D"m_-64521648122302=
21016h5"><br>
&gt; On 14 Nov 2017, at 18:51, Fernando Santagata &lt;<a href=3D"mailto:nan=
do.santagata@gmail.com" target=3D"_blank">nando.santagata@gmail.com</a>&gt;=
 wrote:<br>
&gt;<br>
&gt; Hi Liz,<br>
&gt;<br>
&gt; What I need is to preprocess the value before assigning it to an attri=
bute.<br>
&gt;<br>
&gt; I would do that in Perl5/Moose, using &quot;around&quot;, like this:<b=
r>
&gt;<br>
&gt; package A;<br>
&gt; use Moose;<br>
&gt;<br>
&gt; has &#39;attribute&#39; =3D&gt; (is =3D&gt; &#39;rw&#39;, isa =3D&gt; =
&#39;Str&#39;);<br>
&gt;<br>
&gt; around [qw(attribute)] =3D&gt; sub {<br>
&gt;=C2=A0 =C2=A0my ($next, $self, $val) =3D @_;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next unless $val;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next(preprocess $val); # Preprocess the =
value before the assignment<br>
&gt; }<br>
&gt;<br>
&gt; In this way I don&#39;t have to make an explicit call to the preproces=
sor any time I assign a value to that attribute, effectively removing that =
from the main program.<br>
&gt;<br>
&gt; I&#39;m looking for a way to do that in Perl6.<br>
&gt;<br>
&gt; On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen &lt;<a href=3D"ma=
ilto:liz@dijkmat.nl" target=3D"_blank">liz@dijkmat.nl</a>&gt; wrote:<br>
&gt; &gt; On 14 Nov 2017, at 18:06, Fernando Santagata &lt;<a href=3D"mailt=
o:nando.santagata@gmail.com" target=3D"_blank">nando.santagata@gmail.com</a=
>&gt; wrote:<br>
&gt; &gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; &gt; I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.<br>
&gt; &gt;<br>
&gt; &gt; When I was using Moose, I had some &quot;around&quot; methods tha=
t would automatically modify the value before delivering it to those attrib=
utes, so delegating the object to do the needed adjustments.<br>
&gt; &gt;<br>
&gt; &gt; Stripped to the bare bones, the thing that in Perl6 looks like th=
is:<br>
&gt; &gt;<br>
&gt; &gt; class A {<br>
&gt; &gt;=C2=A0 =C2=A0has $!a;<br>
&gt; &gt;<br>
&gt; &gt;=C2=A0 =C2=A0method a($val?)<br>
&gt; &gt;=C2=A0 =C2=A0{<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt; &gt;=C2=A0 =C2=A0}<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; my A $a .=3D new;<br>
&gt; &gt; # $a.a =3D 42; # This outputs an error<br>
&gt; &gt; $a.a(42);<br>
&gt; &gt; say $a.a;<br>
&gt; &gt;<br>
&gt; &gt; Any hint how to make it work as an assignment, instead of a metho=
d call?<br>
&gt; &gt; Better yet, is there a way to abstract that behavior in a role?<b=
r>
&gt;<br>
&gt; I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0 =C2=A0has $.a is rw;<br>
&gt; }<br>
&gt; my $obj =3D A.new;<br>
&gt; $obj.a =3D 42;<br>
&gt; dd $obj;<br>
&gt; =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
&gt; A $obj =3D A.new(a =3D&gt; 42)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Liz<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Fernando Santagata<br>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata<=
/div>
</div>

--94eb2c13056a711595055df612f3--
0
nando
11/14/2017 7:05:35 PM
--001a113cdfc2cd6f2a055e1649b0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Maybe someone might find this useful.

In the end I concocted this kind of code. The price of simplicity at the
base program level is paid at the class level, with a specific BUILD
submethod, and with an even greater complexity at role level, where the
accessors are created dynamically with some metaprogramming.

role Creator {
  method create(::class, $method, $attribute is rw, &func) {
    ::class.^add_method($method, my method (::class:) {
      Proxy.new(
        FETCH =3D> { $attribute },
        STORE =3D> -> $, $value { $attribute =3D &func($value) }
      )
    });
  }
}

class A does Creator {
  has $!a;
  has $!b;
  submethod BUILD {
    self.create(::A, 'a', $!a, { $^a * 2 });
    self.create(::A, 'b', $!b, { $^a * 3 });
  }
}

my A $a .=3D new;
$a.a =3D 21;
say $a.a;  # 42
$a.b =3D 21;
say $a.b;  # 63


On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth Mattijsen <liz@dijkmat.nl> wrote=
:

> This might it then:
>
> class A {
>     has $!a;  # $.a if you want to be able to assign with .new
>     method a() {
>         Proxy.new(
>           FETCH =3D> { $!a },
>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>         )
>     }
> }
> my $a =3D A.new;
> $a.a =3D 77;
> dd $a.a;   # 154
>
> > On 14 Nov 2017, at 18:51, Fernando Santagata <nando.santagata@gmail.com=
>
> wrote:
> >
> > Hi Liz,
> >
> > What I need is to preprocess the value before assigning it to an
> attribute.
> >
> > I would do that in Perl5/Moose, using "around", like this:
> >
> > package A;
> > use Moose;
> >
> > has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
> >
> > around [qw(attribute)] =3D> sub {
> >   my ($next, $self, $val) =3D @_;
> >   return $self->$next unless $val;
> >   return $self->$next(preprocess $val); # Preprocess the value before
> the assignment
> > }
> >
> > In this way I don't have to make an explicit call to the preprocessor
> any time I assign a value to that attribute, effectively removing that fr=
om
> the main program.
> >
> > I'm looking for a way to do that in Perl6.
> >
> > On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
> wrote:
> > > On 14 Nov 2017, at 18:06, Fernando Santagata <
> nando.santagata@gmail.com> wrote:
> > > I'm converting a program from Perl5/Moose.
> > > I have several classes, each has some attributes that need to be
> processed in the same way before being passed to other objects.
> > >
> > > When I was using Moose, I had some "around" methods that would
> automatically modify the value before delivering it to those attributes, =
so
> delegating the object to do the needed adjustments.
> > >
> > > Stripped to the bare bones, the thing that in Perl6 looks like this:
> > >
> > > class A {
> > >   has $!a;
> > >
> > >   method a($val?)
> > >   {
> > >     if $val.defined {
> > >       # Modify $val in some way
> > >       $!a =3D $val;
> > >     } else {
> > >       $!a;
> > >     }
> > >   }
> > > }
> > >
> > > my A $a .=3D new;
> > > # $a.a =3D 42; # This outputs an error
> > > $a.a(42);
> > > say $a.a;
> > >
> > > Any hint how to make it work as an assignment, instead of a method
> call?
> > > Better yet, is there a way to abstract that behavior in a role?
> >
> > I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
> >
> > class A {
> >     has $.a is rw;
> > }
> > my $obj =3D A.new;
> > $obj.a =3D 42;
> > dd $obj;
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > A $obj =3D A.new(a =3D> 42)
> >
> >
> >
> > Liz
> >
> >
> >
> > --
> > Fernando Santagata
>



--=20
Fernando Santagata

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

<div dir=3D"ltr"><div>Maybe someone might find this useful.</div><div><br><=
/div>In the end I concocted this kind of code. The price of simplicity at t=
he base program level is paid at the class level, with a specific BUILD sub=
method, and with an even greater complexity at role level, where the access=
ors are created dynamically with some metaprogramming.<div><br></div><div><=
div>role Creator {</div><div>=C2=A0 method create(::class, $method, $attrib=
ute is rw, &amp;func) {</div><div>=C2=A0 =C2=A0 ::class.^add_method($method=
, my method (::class:) {</div><div>=C2=A0 =C2=A0 =C2=A0 Proxy.new(</div><di=
v>=C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $attribute },</div><div>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $attribute =3D &am=
p;func($value) }</div><div>=C2=A0 =C2=A0 =C2=A0 )</div><div>=C2=A0 =C2=A0 }=
);</div><div>=C2=A0 }</div><div>}</div><div><br></div><div>class A does Cre=
ator {</div><div>=C2=A0 has $!a;</div><div>=C2=A0 has $!b;</div><div>=C2=A0=
 submethod BUILD {</div><div>=C2=A0 =C2=A0 self.create(::A, &#39;a&#39;, $!=
a, { $^a * 2 });</div><div>=C2=A0 =C2=A0 self.create(::A, &#39;b&#39;, $!b,=
 { $^a * 3 });</div><div>=C2=A0 }</div><div>}</div><div><br></div><div>my A=
 $a .=3D new;</div><div>$a.a =3D 21;</div><div>say $a.a;=C2=A0 # 42</div><d=
iv>$a.b =3D 21;</div><div>say $a.b;=C2=A0 # 63</div></div><div><br></div></=
div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Tue, Nov 1=
4, 2017 at 7:09 PM, Elizabeth Mattijsen <span dir=3D"ltr">&lt;<a href=3D"ma=
ilto:liz@dijkmat.nl" target=3D"_blank">liz@dijkmat.nl</a>&gt;</span> wrote:=
<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-lef=
t:1px #ccc solid;padding-left:1ex">This might it then:<br>
<br>
class A {<br>
=C2=A0 =C2=A0 has $!a;=C2=A0 # $.a if you want to be able to assign with .n=
ew<br>
=C2=A0 =C2=A0 method a() {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { $!a },<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value { $!a =3D =
$value * 2 }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
=C2=A0 =C2=A0 }<br>
}<br>
my $a =3D A.new;<br>
$a.a =3D 77;<br>
dd $a.a;=C2=A0 =C2=A0# 154<br>
<div class=3D"HOEnZb"><div class=3D"h5"><br>
&gt; On 14 Nov 2017, at 18:51, Fernando Santagata &lt;<a href=3D"mailto:nan=
do.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi Liz,<br>
&gt;<br>
&gt; What I need is to preprocess the value before assigning it to an attri=
bute.<br>
&gt;<br>
&gt; I would do that in Perl5/Moose, using &quot;around&quot;, like this:<b=
r>
&gt;<br>
&gt; package A;<br>
&gt; use Moose;<br>
&gt;<br>
&gt; has &#39;attribute&#39; =3D&gt; (is =3D&gt; &#39;rw&#39;, isa =3D&gt; =
&#39;Str&#39;);<br>
&gt;<br>
&gt; around [qw(attribute)] =3D&gt; sub {<br>
&gt;=C2=A0 =C2=A0my ($next, $self, $val) =3D @_;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next unless $val;<br>
&gt;=C2=A0 =C2=A0return $self-&gt;$next(preprocess $val); # Preprocess the =
value before the assignment<br>
&gt; }<br>
&gt;<br>
&gt; In this way I don&#39;t have to make an explicit call to the preproces=
sor any time I assign a value to that attribute, effectively removing that =
from the main program.<br>
&gt;<br>
&gt; I&#39;m looking for a way to do that in Perl6.<br>
&gt;<br>
&gt; On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen &lt;<a href=3D"ma=
ilto:liz@dijkmat.nl">liz@dijkmat.nl</a>&gt; wrote:<br>
&gt; &gt; On 14 Nov 2017, at 18:06, Fernando Santagata &lt;<a href=3D"mailt=
o:nando.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt; &gt; I&#39;m converting a program from Perl5/Moose.<br>
&gt; &gt; I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.<br>
&gt; &gt;<br>
&gt; &gt; When I was using Moose, I had some &quot;around&quot; methods tha=
t would automatically modify the value before delivering it to those attrib=
utes, so delegating the object to do the needed adjustments.<br>
&gt; &gt;<br>
&gt; &gt; Stripped to the bare bones, the thing that in Perl6 looks like th=
is:<br>
&gt; &gt;<br>
&gt; &gt; class A {<br>
&gt; &gt;=C2=A0 =C2=A0has $!a;<br>
&gt; &gt;<br>
&gt; &gt;=C2=A0 =C2=A0method a($val?)<br>
&gt; &gt;=C2=A0 =C2=A0{<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0if $val.defined {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0# Modify $val in some way<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a =3D $val;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0} else {<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0$!a;<br>
&gt; &gt;=C2=A0 =C2=A0 =C2=A0}<br>
&gt; &gt;=C2=A0 =C2=A0}<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; my A $a .=3D new;<br>
&gt; &gt; # $a.a =3D 42; # This outputs an error<br>
&gt; &gt; $a.a(42);<br>
&gt; &gt; say $a.a;<br>
&gt; &gt;<br>
&gt; &gt; Any hint how to make it work as an assignment, instead of a metho=
d call?<br>
&gt; &gt; Better yet, is there a way to abstract that behavior in a role?<b=
r>
&gt;<br>
&gt; I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?<br>
&gt;<br>
&gt; class A {<br>
&gt;=C2=A0 =C2=A0 =C2=A0has $.a is rw;<br>
&gt; }<br>
&gt; my $obj =3D A.new;<br>
&gt; $obj.a =3D 42;<br>
&gt; dd $obj;<br>
&gt; =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
&gt; A $obj =3D A.new(a =3D&gt; 42)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Liz<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Fernando Santagata<br>
</div></div></blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>=
<div class=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando =
Santagata</div>
</div>

--001a113cdfc2cd6f2a055e1649b0--
0
nando
11/16/2017 9:31:43 AM
You could probably do this nicer as an Addtribute trait:

Creator.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
sub trait_mod:<is>(\attribute, :&proxy!) is export {
    attribute.package.^add_method(attribute.name,my method () { =
proxy($_) })
}

A.pm
=3D=3D=3D=3D
use Creator;

class A {
    has $!a is proxy( &func );
    has $!b is proxy( &func );
}

Didn=E2=80=99t test this thoroughly, but this should give you some idea. =
 :-)


Liz

> On 16 Nov 2017, at 10:31, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
>=20
> Maybe someone might find this useful.
>=20
> In the end I concocted this kind of code. The price of simplicity at =
the base program level is paid at the class level, with a specific BUILD =
submethod, and with an even greater complexity at role level, where the =
accessors are created dynamically with some metaprogramming.
>=20
> role Creator {
>   method create(::class, $method, $attribute is rw, &func) {
>     ::class.^add_method($method, my method (::class:) {
>       Proxy.new(
>         FETCH =3D> { $attribute },
>         STORE =3D> -> $, $value { $attribute =3D &func($value) }
>       )
>     });
>   }
> }
>=20
> class A does Creator {
>   has $!a;
>   has $!b;
>   submethod BUILD {
>     self.create(::A, 'a', $!a, { $^a * 2 });
>     self.create(::A, 'b', $!b, { $^a * 3 });
>   }
> }
>=20
> my A $a .=3D new;
> $a.a =3D 21;
> say $a.a;  # 42
> $a.b =3D 21;
> say $a.b;  # 63
>=20
>=20
> On Tue, Nov 14, 2017 at 7:09 PM, Elizabeth Mattijsen <liz@dijkmat.nl> =
wrote:
> This might it then:
>=20
> class A {
>     has $!a;  # $.a if you want to be able to assign with .new
>     method a() {
>         Proxy.new(
>           FETCH =3D> { $!a },
>           STORE =3D> -> $, $value { $!a =3D $value * 2 }
>         )
>     }
> }
> my $a =3D A.new;
> $a.a =3D 77;
> dd $a.a;   # 154
>=20
> > On 14 Nov 2017, at 18:51, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> >
> > Hi Liz,
> >
> > What I need is to preprocess the value before assigning it to an =
attribute.
> >
> > I would do that in Perl5/Moose, using "around", like this:
> >
> > package A;
> > use Moose;
> >
> > has 'attribute' =3D> (is =3D> 'rw', isa =3D> 'Str');
> >
> > around [qw(attribute)] =3D> sub {
> >   my ($next, $self, $val) =3D @_;
> >   return $self->$next unless $val;
> >   return $self->$next(preprocess $val); # Preprocess the value =
before the assignment
> > }
> >
> > In this way I don't have to make an explicit call to the =
preprocessor any time I assign a value to that attribute, effectively =
removing that from the main program.
> >
> > I'm looking for a way to do that in Perl6.
> >
> > On Tue, Nov 14, 2017 at 6:11 PM, Elizabeth Mattijsen =
<liz@dijkmat.nl> wrote:
> > > On 14 Nov 2017, at 18:06, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> > > I'm converting a program from Perl5/Moose.
> > > I have several classes, each has some attributes that need to be =
processed in the same way before being passed to other objects.
> > >
> > > When I was using Moose, I had some "around" methods that would =
automatically modify the value before delivering it to those attributes, =
so delegating the object to do the needed adjustments.
> > >
> > > Stripped to the bare bones, the thing that in Perl6 looks like =
this:
> > >
> > > class A {
> > >   has $!a;
> > >
> > >   method a($val?)
> > >   {
> > >     if $val.defined {
> > >       # Modify $val in some way
> > >       $!a =3D $val;
> > >     } else {
> > >       $!a;
> > >     }
> > >   }
> > > }
> > >
> > > my A $a .=3D new;
> > > # $a.a =3D 42; # This outputs an error
> > > $a.a(42);
> > > say $a.a;
> > >
> > > Any hint how to make it work as an assignment, instead of a method =
call?
> > > Better yet, is there a way to abstract that behavior in a role?
> >
> > I think you want =E2=80=9Cis rw=E2=80=9D on a public attribute?
> >
> > class A {
> >     has $.a is rw;
> > }
> > my $obj =3D A.new;
> > $obj.a =3D 42;
> > dd $obj;
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > A $obj =3D A.new(a =3D> 42)
> >
> >
> >
> > Liz
> >
> >
> >
> > --
> > Fernando Santagata
>=20
>=20
>=20
> --=20
> Fernando Santagata
0
liz
11/17/2017 12:24:20 AM
Oops,

> On 17 Nov 2017, at 01:24, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
> sub trait_mod:<is>(\attribute, :&proxy!) is export {

sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) is export {

so that the trait can only be used on Attribute objects.



Liz
0
liz
11/17/2017 12:27:09 AM
--001a11c185a2146ab7055e327a0e
Content-Type: text/plain; charset="UTF-8"

Hi Liz,

I tried to use the code you suggested:

sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
  attribute.package.^add_method(attribute.name, my method () { proxy($_) })
}

class A {
  has $!a is proxy({ $^a * 2 });
  has $!b is proxy({ $^a * 3 });
}

my A $a .= new;
$a.a = 21;    # <-- No such method 'a' for invocant of type 'A'
say $a.a;
$a.b = 21;
say $a.b;

But I got an error on the first assignment.
What am I doing wrong?

On Fri, Nov 17, 2017 at 1:27 AM, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:

> Oops,
>
> > On 17 Nov 2017, at 01:24, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
> > sub trait_mod:<is>(\attribute, :&proxy!) is export {
>
> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) is export {
>
> so that the trait can only be used on Attribute objects.
>
>
>
> Liz
>



-- 
Fernando Santagata

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

<div dir=3D"ltr"><div>Hi Liz,</div><div><br></div>I tried to use the code y=
ou suggested:<div><br></div><div><div>sub trait_mod:&lt;is&gt;(Attribute:D =
\attribute, :&amp;proxy!) {</div><div>=C2=A0 attribute.package.^add_method(=
<a href=3D"http://attribute.name">attribute.name</a>, my method () { proxy(=
$_) })</div><div>}</div><div><br></div><div>class A {</div><div>=C2=A0 has =
$!a is proxy({ $^a * 2 });</div><div>=C2=A0 has $!b is proxy({ $^a * 3 });<=
/div><div>}</div><div><br></div><div>my A $a .=3D new;</div><div>$a.a =3D 2=
1;=C2=A0 =C2=A0 # &lt;--=C2=A0No such method &#39;a&#39; for invocant of ty=
pe &#39;A&#39;</div><div>say $a.a;</div><div>$a.b =3D 21;</div><div>say $a.=
b;</div></div><div><br></div><div>But I got an error on the first assignmen=
t.</div><div>What am I doing wrong?</div></div><div class=3D"gmail_extra"><=
br><div class=3D"gmail_quote">On Fri, Nov 17, 2017 at 1:27 AM, Elizabeth Ma=
ttijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"_=
blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_q=
uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e=
x">Oops,<br>
<span class=3D""><br>
&gt; On 17 Nov 2017, at 01:24, Elizabeth Mattijsen &lt;<a href=3D"mailto:li=
z@dijkmat.nl">liz@dijkmat.nl</a>&gt; wrote:<br>
&gt; sub trait_mod:&lt;is&gt;(\attribute, :&amp;proxy!) is export {<br>
<br>
</span>sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;proxy!) is ex=
port {<br>
<br>
so that the trait can only be used on Attribute objects.<br>
<br>
<br>
<br>
Liz<br>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata<=
/div>
</div>

--001a11c185a2146ab7055e327a0e--
0
nando
11/17/2017 7:09:32 PM
> On 17 Nov 2017, at 20:09, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> I tried to use the code you suggested:
>=20
> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>   attribute.package.^add_method(attribute.name, my method () { =
proxy($_) })
> }
>=20
> class A {
>   has $!a is proxy({ $^a * 2 });
>   has $!b is proxy({ $^a * 3 });
> }
>=20
> my A $a .=3D new;
> $a.a =3D 21;    # <-- No such method 'a' for invocant of type =E2=80=98A=
'

The problem here is that I forgot that the name of the attribute =
includes the sigil and the twigil.  Which you don=E2=80=99t want to be =
part of the method name.

attribute.name.substr(2) fixes this, but then other problems appear=E2=80=A6=



Looking into that now...


Liz=
0
liz
11/17/2017 9:31:33 PM
> On 17 Nov 2017, at 20:09, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> I tried to use the code you suggested:
>=20
> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>   attribute.package.^add_method(attribute.name, my method () { =
proxy($_) })
> }
>=20
> class A {
>   has $!a is proxy({ $^a * 2 });
>   has $!b is proxy({ $^a * 3 });
> }
>=20
> my A $a .=3D new;
> $a.a =3D 21;    # <-- No such method 'a' for invocant of type 'A'
> say $a.a;
> $a.b =3D 21;
> say $a.b;
>=20
> But I got an error on the first assignment.
> What am I doing wrong?

Ok, finally had some time to figure this out more deeply.  My result:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
    my $name   =3D attribute.name;
    my $method =3D $name.substr(2);
    attribute.package.^add_method($method, my method ($SELF:) is raw {
        use MONKEY-GUTS;
        Proxy.new(
          FETCH =3D> { nqp::getattr(nqp::decont($SELF),$SELF.WHAT,$name) =
},
          STORE =3D> -> $, $value {
            =
nqp::bindattr(nqp::decont($SELF),$SELF.WHAT,$name,proxy($value))
          }
        )
    });
}

class A {
      has $!a is proxy({ $^a * 2 });
      has $!b is proxy({ $^a * 3 });
}

my A $a .=3D new;
$a.a =3D 21;   =20
say $a.a;   # 42
$a.b =3D 21;
say $a.b;   # 63
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Unfortunately, this involves some MONKEY guts=E2=80=A6  lemme see if I =
can figure out a way for you not to have to do that.



Liz=
0
liz
11/17/2017 9:47:04 PM
> On 17 Nov 2017, at 22:47, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
>> On 17 Nov 2017, at 20:09, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
>> I tried to use the code you suggested:
>>=20
>> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>>  attribute.package.^add_method(attribute.name, my method () { =
proxy($_) })
>> }
>>=20
>> class A {
>>  has $!a is proxy({ $^a * 2 });
>>  has $!b is proxy({ $^a * 3 });
>> }
>>=20
>> my A $a .=3D new;
>> $a.a =3D 21;    # <-- No such method 'a' for invocant of type 'A'
>> say $a.a;
>> $a.b =3D 21;
>> say $a.b;
>>=20
>> But I got an error on the first assignment.
>> What am I doing wrong?
>=20
> Ok, finally had some time to figure this out more deeply.  My result:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>    my $name   =3D attribute.name;
>    my $method =3D $name.substr(2);
>    attribute.package.^add_method($method, my method ($SELF:) is raw {
>        use MONKEY-GUTS;
>        Proxy.new(
>          FETCH =3D> { =
nqp::getattr(nqp::decont($SELF),$SELF.WHAT,$name) },
>          STORE =3D> -> $, $value {
>            =
nqp::bindattr(nqp::decont($SELF),$SELF.WHAT,$name,proxy($value))
>          }
>        )
>    });
> }

And this would be the version without MONKEY-GUTS, moritz++

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
    attribute.package.^add_method(attribute.name.substr(2), my method =
($SELF:) {
        Proxy.new(
          FETCH =3D> { attribute.get_value($SELF) },
          STORE =3D> -> $, \value { =
attribute.set_value($SELF,proxy(value)) }
        )
    });
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Turns out the Attribute class has a set_value and get_value method.  =
Which moritz++ pointed out to me on #perl6-dev.  So no need to get =
gutsy!



Liz=
0
liz
11/17/2017 10:14:53 PM
--94eb2c04f9e4146cc3055e45209c
Content-Type: text/plain; charset="UTF-8"

Hi Liz,

Thank you and my regards to Moritz too!

On Fri, Nov 17, 2017 at 11:14 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
wrote:

> > On 17 Nov 2017, at 22:47, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
> >> On 17 Nov 2017, at 20:09, Fernando Santagata <nando.santagata@gmail.com>
> wrote:
> >> I tried to use the code you suggested:
> >>
> >> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
> >>  attribute.package.^add_method(attribute.name, my method () {
> proxy($_) })
> >> }
> >>
> >> class A {
> >>  has $!a is proxy({ $^a * 2 });
> >>  has $!b is proxy({ $^a * 3 });
> >> }
> >>
> >> my A $a .= new;
> >> $a.a = 21;    # <-- No such method 'a' for invocant of type 'A'
> >> say $a.a;
> >> $a.b = 21;
> >> say $a.b;
> >>
> >> But I got an error on the first assignment.
> >> What am I doing wrong?
> >
> > Ok, finally had some time to figure this out more deeply.  My result:
> > =====================================
> > sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
> >    my $name   = attribute.name;
> >    my $method = $name.substr(2);
> >    attribute.package.^add_method($method, my method ($SELF:) is raw {
> >        use MONKEY-GUTS;
> >        Proxy.new(
> >          FETCH => { nqp::getattr(nqp::decont($SELF),$SELF.WHAT,$name) },
> >          STORE => -> $, $value {
> >            nqp::bindattr(nqp::decont($SELF),$SELF.WHAT,$name,proxy($
> value))
> >          }
> >        )
> >    });
> > }
>
> And this would be the version without MONKEY-GUTS, moritz++
>
> ====================================
> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>     attribute.package.^add_method(attribute.name.substr(2), my method
> ($SELF:) {
>         Proxy.new(
>           FETCH => { attribute.get_value($SELF) },
>           STORE => -> $, \value { attribute.set_value($SELF,proxy(value))
> }
>         )
>     });
> }
> ====================================
>
> Turns out the Attribute class has a set_value and get_value method.  Which
> moritz++ pointed out to me on #perl6-dev.  So no need to get gutsy!
>
>
>
> Liz




-- 
Fernando Santagata

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

<div dir=3D"ltr">Hi Liz,<div><br></div><div>Thank you and my regards to Mor=
itz too!</div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quot=
e">On Fri, Nov 17, 2017 at 11:14 PM, Elizabeth Mattijsen <span dir=3D"ltr">=
&lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"_blank">liz@dijkmat.nl</a>&=
gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 =
0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class=3D"HOEnZb"><=
div class=3D"h5">&gt; On 17 Nov 2017, at 22:47, Elizabeth Mattijsen &lt;<a =
href=3D"mailto:liz@dijkmat.nl">liz@dijkmat.nl</a>&gt; wrote:<br>
&gt;&gt; On 17 Nov 2017, at 20:09, Fernando Santagata &lt;<a href=3D"mailto=
:nando.santagata@gmail.com">nando.santagata@gmail.com</a>&gt; wrote:<br>
&gt;&gt; I tried to use the code you suggested:<br>
&gt;&gt;<br>
&gt;&gt; sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;proxy!) {<b=
r>
&gt;&gt;=C2=A0 attribute.package.^add_method(<a href=3D"http://attribute.na=
me" rel=3D"noreferrer" target=3D"_blank"><wbr>attribute.name</a>, my method=
 () { proxy($_) })<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; class A {<br>
&gt;&gt;=C2=A0 has $!a is proxy({ $^a * 2 });<br>
&gt;&gt;=C2=A0 has $!b is proxy({ $^a * 3 });<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; my A $a .=3D new;<br>
&gt;&gt; $a.a =3D 21;=C2=A0 =C2=A0 # &lt;-- No such method &#39;a&#39; for =
invocant of type &#39;A&#39;<br>
&gt;&gt; say $a.a;<br>
&gt;&gt; $a.b =3D 21;<br>
&gt;&gt; say $a.b;<br>
&gt;&gt;<br>
&gt;&gt; But I got an error on the first assignment.<br>
&gt;&gt; What am I doing wrong?<br>
&gt;<br>
&gt; Ok, finally had some time to figure this out more deeply.=C2=A0 My res=
ult:<br>
&gt; =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D<wbr>=3D=3D=3D=3D=3D=3D=3D<br>
&gt; sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;proxy!) {<br>
&gt;=C2=A0 =C2=A0 my $name=C2=A0 =C2=A0=3D <a href=3D"http://attribute.name=
" rel=3D"noreferrer" target=3D"_blank">attribute.name</a>;<br>
&gt;=C2=A0 =C2=A0 my $method =3D $name.substr(2);<br>
&gt;=C2=A0 =C2=A0 attribute.package.^add_method(<wbr>$method, my method ($S=
ELF:) is raw {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 use MONKEY-GUTS;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { nqp::getattr(nqp::de=
cont($<wbr>SELF),$SELF.WHAT,$name) },<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 nqp::bindattr(nqp::decont($<w=
br>SELF),$SELF.WHAT,$name,proxy($<wbr>value))<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
&gt;=C2=A0 =C2=A0 });<br>
&gt; }<br>
<br>
</div></div>And this would be the version without MONKEY-GUTS, moritz++<br>
<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<wbr>=3D=3D=3D=3D=3D=3D<br>
<span class=3D"">sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;pro=
xy!) {<br>
</span>=C2=A0 =C2=A0 attribute.package.^add_method(<wbr>attribute.name.subs=
tr(2), my method ($SELF:) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { attribute.get_value($SEL=
F) },<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, \value { attribut=
e.set_value($SELF,<wbr>proxy(value)) }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
=C2=A0 =C2=A0 });<br>
}<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<wbr>=3D=3D=3D=3D=3D=3D<br>
<br>
Turns out the Attribute class has a set_value and get_value method.=C2=A0 W=
hich moritz++ pointed out to me on #perl6-dev.=C2=A0 So no need to get guts=
y!<br>
<br>
<br>
<br>
Liz</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata=
</div>
</div>

--94eb2c04f9e4146cc3055e45209c--
0
nando
11/18/2017 5:24:27 PM
--94eb2c04f9e4f7b41c055e461747
Content-Type: text/plain; charset="UTF-8"

The last iteration of this code snippet is:

multi sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
  attribute.package.^add_method(attribute.name.substr(2), my method
($SELF:) {
    Proxy.new(
      FETCH => { attribute.get_value($SELF) },
      STORE => -> $, \value { attribute.set_value($SELF,proxy(value)) }
    )
  });
}

class A {
  has $.test is rw;
  has $!a is proxy({ $^a * 2 });
  has $!b is proxy({ $^a * 3 });
}

my A $a .= new;
$a.a = 21;
say $a.a;
$a.b = 21;
say $a.b;

The "multi" is necessary, otherwise the declared "is proxy" trait becomes
the only one possible and the "is rw" trait of the $.test attribute
generates a compile-time error.

On Sat, Nov 18, 2017 at 6:24 PM, Fernando Santagata <
nando.santagata@gmail.com> wrote:

> Hi Liz,
>
> Thank you and my regards to Moritz too!
>
> On Fri, Nov 17, 2017 at 11:14 PM, Elizabeth Mattijsen <liz@dijkmat.nl>
> wrote:
>
>> > On 17 Nov 2017, at 22:47, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
>> >> On 17 Nov 2017, at 20:09, Fernando Santagata <
>> nando.santagata@gmail.com> wrote:
>> >> I tried to use the code you suggested:
>> >>
>> >> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>> >>  attribute.package.^add_method(attribute.name, my method () {
>> proxy($_) })
>> >> }
>> >>
>> >> class A {
>> >>  has $!a is proxy({ $^a * 2 });
>> >>  has $!b is proxy({ $^a * 3 });
>> >> }
>> >>
>> >> my A $a .= new;
>> >> $a.a = 21;    # <-- No such method 'a' for invocant of type 'A'
>> >> say $a.a;
>> >> $a.b = 21;
>> >> say $a.b;
>> >>
>> >> But I got an error on the first assignment.
>> >> What am I doing wrong?
>> >
>> > Ok, finally had some time to figure this out more deeply.  My result:
>> > =====================================
>> > sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>> >    my $name   = attribute.name;
>> >    my $method = $name.substr(2);
>> >    attribute.package.^add_method($method, my method ($SELF:) is raw {
>> >        use MONKEY-GUTS;
>> >        Proxy.new(
>> >          FETCH => { nqp::getattr(nqp::decont($SELF),$SELF.WHAT,$name)
>> },
>> >          STORE => -> $, $value {
>> >            nqp::bindattr(nqp::decont($SELF),$SELF.WHAT,$name,proxy($val
>> ue))
>> >          }
>> >        )
>> >    });
>> > }
>>
>> And this would be the version without MONKEY-GUTS, moritz++
>>
>> ====================================
>> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>>     attribute.package.^add_method(attribute.name.substr(2), my method
>> ($SELF:) {
>>         Proxy.new(
>>           FETCH => { attribute.get_value($SELF) },
>>           STORE => -> $, \value { attribute.set_value($SELF,proxy(value))
>> }
>>         )
>>     });
>> }
>> ====================================
>>
>> Turns out the Attribute class has a set_value and get_value method.
>> Which moritz++ pointed out to me on #perl6-dev.  So no need to get gutsy!
>>
>>
>>
>> Liz
>
>
>
>
> --
> Fernando Santagata
>



-- 
Fernando Santagata

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

<div dir=3D"ltr">The last iteration of this code snippet is:<div><br></div>=
<div><div>multi sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;prox=
y!) {</div><div>=C2=A0 attribute.package.^add_method(attribute.name.substr(=
2), my method ($SELF:) {</div><div>=C2=A0 =C2=A0 Proxy.new(</div><div>=C2=
=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { attribute.get_value($SELF) },</div><div>=
=C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, \value { attribute.set_value($S=
ELF,proxy(value)) }</div><div>=C2=A0 =C2=A0 )</div><div>=C2=A0 });</div><di=
v>}</div><div><br></div><div>class A {</div><div>=C2=A0 has $.test is rw;</=
div><div>=C2=A0 has $!a is proxy({ $^a * 2 });</div><div>=C2=A0 has $!b is =
proxy({ $^a * 3 });</div><div>}</div><div><br></div><div>my A $a .=3D new;<=
/div><div>$a.a =3D 21;</div><div>say $a.a;</div><div>$a.b =3D 21;</div><div=
>say $a.b;</div></div><div><br></div><div>The &quot;multi&quot; is necessar=
y, otherwise the declared &quot;is proxy&quot; trait becomes the only one p=
ossible and the &quot;is rw&quot; trait of the $.test attribute generates a=
 compile-time error.</div></div><div class=3D"gmail_extra"><br><div class=
=3D"gmail_quote">On Sat, Nov 18, 2017 at 6:24 PM, Fernando Santagata <span =
dir=3D"ltr">&lt;<a href=3D"mailto:nando.santagata@gmail.com" target=3D"_bla=
nk">nando.santagata@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D=
"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding=
-left:1ex"><div dir=3D"ltr">Hi Liz,<div><br></div><div>Thank you and my reg=
ards to Moritz too!</div></div><div class=3D"gmail_extra"><div><div class=
=3D"h5"><br><div class=3D"gmail_quote">On Fri, Nov 17, 2017 at 11:14 PM, El=
izabeth Mattijsen <span dir=3D"ltr">&lt;<a href=3D"mailto:liz@dijkmat.nl" t=
arget=3D"_blank">liz@dijkmat.nl</a>&gt;</span> wrote:<br><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div class=3D"m_-2844944127463008488HOEnZb"><div class=3D"m_-=
2844944127463008488h5">&gt; On 17 Nov 2017, at 22:47, Elizabeth Mattijsen &=
lt;<a href=3D"mailto:liz@dijkmat.nl" target=3D"_blank">liz@dijkmat.nl</a>&g=
t; wrote:<br>
&gt;&gt; On 17 Nov 2017, at 20:09, Fernando Santagata &lt;<a href=3D"mailto=
:nando.santagata@gmail.com" target=3D"_blank">nando.santagata@gmail.com</a>=
&gt; wrote:<br>
&gt;&gt; I tried to use the code you suggested:<br>
&gt;&gt;<br>
&gt;&gt; sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;proxy!) {<b=
r>
&gt;&gt;=C2=A0 attribute.package.^add_method(<a href=3D"http://attribute.na=
me" rel=3D"noreferrer" target=3D"_blank"><wbr>attribute.name</a>, my method=
 () { proxy($_) })<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; class A {<br>
&gt;&gt;=C2=A0 has $!a is proxy({ $^a * 2 });<br>
&gt;&gt;=C2=A0 has $!b is proxy({ $^a * 3 });<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; my A $a .=3D new;<br>
&gt;&gt; $a.a =3D 21;=C2=A0 =C2=A0 # &lt;-- No such method &#39;a&#39; for =
invocant of type &#39;A&#39;<br>
&gt;&gt; say $a.a;<br>
&gt;&gt; $a.b =3D 21;<br>
&gt;&gt; say $a.b;<br>
&gt;&gt;<br>
&gt;&gt; But I got an error on the first assignment.<br>
&gt;&gt; What am I doing wrong?<br>
&gt;<br>
&gt; Ok, finally had some time to figure this out more deeply.=C2=A0 My res=
ult:<br>
&gt; =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D<wbr>=3D=3D=3D=3D=3D=3D=3D<br>
&gt; sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;proxy!) {<br>
&gt;=C2=A0 =C2=A0 my $name=C2=A0 =C2=A0=3D <a href=3D"http://attribute.name=
" rel=3D"noreferrer" target=3D"_blank">attribute.name</a>;<br>
&gt;=C2=A0 =C2=A0 my $method =3D $name.substr(2);<br>
&gt;=C2=A0 =C2=A0 attribute.package.^add_method(<wbr>$method, my method ($S=
ELF:) is raw {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 use MONKEY-GUTS;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { nqp::getattr(nqp::de=
cont($SELF<wbr>),$SELF.WHAT,$name) },<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, $value {<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 nqp::bindattr(nqp::decont($SE=
L<wbr>F),$SELF.WHAT,$name,proxy($val<wbr>ue))<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
&gt;=C2=A0 =C2=A0 });<br>
&gt; }<br>
<br>
</div></div>And this would be the version without MONKEY-GUTS, moritz++<br>
<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<wbr>=3D=3D=3D=3D=3D=3D<br>
<span>sub trait_mod:&lt;is&gt;(Attribute:D \attribute, :&amp;proxy!) {<br>
</span>=C2=A0 =C2=A0 attribute.package.^add_method(<wbr>attribute.name.subs=
tr(2), my method ($SELF:) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Proxy.new(<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FETCH =3D&gt; { attribute.get_value($SEL=
F) },<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 STORE =3D&gt; -&gt; $, \value { attribut=
e.set_value($SELF,prox<wbr>y(value)) }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )<br>
=C2=A0 =C2=A0 });<br>
}<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<wbr>=3D=3D=3D=3D=3D=3D<br>
<br>
Turns out the Attribute class has a set_value and get_value method.=C2=A0 W=
hich moritz++ pointed out to me on #perl6-dev.=C2=A0 So no need to get guts=
y!<br>
<br>
<br>
<br>
Liz</blockquote></div><br><br clear=3D"all"><div><br></div></div></div><spa=
n class=3D"HOEnZb"><font color=3D"#888888">-- <br><div class=3D"m_-28449441=
27463008488gmail_signature" data-smartmail=3D"gmail_signature">Fernando San=
tagata</div>
</font></span></div>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata<=
/div>
</div>

--94eb2c04f9e4f7b41c055e461747--
0
nando
11/18/2017 6:33:45 PM
Ah, duh, yes, should be a multi  :-)

> On 18 Nov 2017, at 19:33, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
>=20
> The last iteration of this code snippet is:
>=20
> multi sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>   attribute.package.^add_method(attribute.name.substr(2), my method =
($SELF:) {
>     Proxy.new(
>       FETCH =3D> { attribute.get_value($SELF) },
>       STORE =3D> -> $, \value { =
attribute.set_value($SELF,proxy(value)) }
>     )
>   });
> }
>=20
> class A {
>   has $.test is rw;
>   has $!a is proxy({ $^a * 2 });
>   has $!b is proxy({ $^a * 3 });
> }
>=20
> my A $a .=3D new;
> $a.a =3D 21;
> say $a.a;
> $a.b =3D 21;
> say $a.b;
>=20
> The "multi" is necessary, otherwise the declared "is proxy" trait =
becomes the only one possible and the "is rw" trait of the $.test =
attribute generates a compile-time error.
>=20
> On Sat, Nov 18, 2017 at 6:24 PM, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> Hi Liz,
>=20
> Thank you and my regards to Moritz too!
>=20
> On Fri, Nov 17, 2017 at 11:14 PM, Elizabeth Mattijsen <liz@dijkmat.nl> =
wrote:
> > On 17 Nov 2017, at 22:47, Elizabeth Mattijsen <liz@dijkmat.nl> =
wrote:
> >> On 17 Nov 2017, at 20:09, Fernando Santagata =
<nando.santagata@gmail.com> wrote:
> >> I tried to use the code you suggested:
> >>
> >> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
> >>  attribute.package.^add_method(attribute.name, my method () { =
proxy($_) })
> >> }
> >>
> >> class A {
> >>  has $!a is proxy({ $^a * 2 });
> >>  has $!b is proxy({ $^a * 3 });
> >> }
> >>
> >> my A $a .=3D new;
> >> $a.a =3D 21;    # <-- No such method 'a' for invocant of type 'A'
> >> say $a.a;
> >> $a.b =3D 21;
> >> say $a.b;
> >>
> >> But I got an error on the first assignment.
> >> What am I doing wrong?
> >
> > Ok, finally had some time to figure this out more deeply.  My =
result:
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
> >    my $name   =3D attribute.name;
> >    my $method =3D $name.substr(2);
> >    attribute.package.^add_method($method, my method ($SELF:) is raw =
{
> >        use MONKEY-GUTS;
> >        Proxy.new(
> >          FETCH =3D> { =
nqp::getattr(nqp::decont($SELF),$SELF.WHAT,$name) },
> >          STORE =3D> -> $, $value {
> >            =
nqp::bindattr(nqp::decont($SELF),$SELF.WHAT,$name,proxy($value))
> >          }
> >        )
> >    });
> > }
>=20
> And this would be the version without MONKEY-GUTS, moritz++
>=20
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> sub trait_mod:<is>(Attribute:D \attribute, :&proxy!) {
>     attribute.package.^add_method(attribute.name.substr(2), my method =
($SELF:) {
>         Proxy.new(
>           FETCH =3D> { attribute.get_value($SELF) },
>           STORE =3D> -> $, \value { =
attribute.set_value($SELF,proxy(value)) }
>         )
>     });
> }
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>=20
> Turns out the Attribute class has a set_value and get_value method.  =
Which moritz++ pointed out to me on #perl6-dev.  So no need to get =
gutsy!
>=20
>=20
>=20
> Liz
>=20
>=20
>=20
> --=20
> Fernando Santagata
>=20
>=20
>=20
> --=20
> Fernando Santagata
0
liz
11/18/2017 9:45:53 PM
Reply: