🦋 Welcome to the MAIN() IRC channel of the Raku Programming Language (raku.org). This channel is logged for the purpose of keeping a history about its development | evalbot usage: 'm: say 3;' or /msg camelia m: ... | Log inspection is getting closer to beta. If you're a beginner, you can also check out the #raku-beginner channel!
Set by lizmat on 25 August 2021.
hankache m: say "Hello #raku" 16:58
camelia Hello #raku 16:59
tellable6 2020-06-01T18:36:00Z #raku-dev <tyil> .tell hankache maybe you can help here stackoverflow.com/questions/621372...in-windows
El_Che hankache: it's been a while :) 17:24
hankache El_Che indeed it has! 17:28
I hope all is well :) 17:29
El_Che :) 17:38
grondilu Hi. Is there such a thing as a mutator method? One that would define the semantics for `$instance.rw-attribute = $some-value` ? 21:23
I guess I can write a `set-attribute` method. 21:32
Voldenet grondilu: this could be applicable docs.raku.org/type/Proxy 21:35
I've checked, it's not working as I expected 21:37
grondilu Voldenet: that might be what I need, though. What do you mean it's not working? Is it broken or something? 21:44
m: my $x; my $p = Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { $x = $y }; $p = 5; say $p; 21:46
camelia Use of uninitialized value of type Any in numeric context
5
in method <anon> at <tmp> line 1
grondilu m: my $x = 2; my $p = Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { $x = $y }; $p = 5; say $p;
camelia 5
grondilu 🤔 21:47
meh, I can write a set method until I figure out this. 21:48
still... 21:50
m: my $x = 2; my $p = Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { note "using STORE!"; $x = $y }; $p = 5; say $p;
camelia 5
grondilu m: my $x = 2; my $p = Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { $x = $y }; $p = 5; say $p ~~ Proxy;
camelia False
japhb Bind the Proxy to $p, I think 21:56
Otherwise you're sticking a Proxy inside a Scalar, and the Scalar's assignment semantics win. 21:57
m: my $x = 2; my $p := Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { $x = $y }; $p = 5; say $p ~~ Proxy; 21:58
camelia False
japhb m: my $x = 2; my $p := Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { $x = $y }; $p = 5; say $p
camelia 10
japhb m: my $x = 2; my $p := Proxy.new: FETCH => method { 2*$x }, STORE => method ($y) { $x = $y }; $p = 5; say $p.VAR ~~ Proxy
camelia True
japhb grondilu: ^^ 21:59
grondilu japhb: noted, thanks
grondilu goes to sleep 22:00
Voldenet grondilu: by `not working as expected` I meant storing Proxy in an attribute, it's possible to use it if you can accept that accessing the setter always allocates the proxy 22:16
tellable6 Voldenet, I'll pass your message to grondilu
grondilu is back, couldn't sleep. 23:34
tellable6 2022-05-05T22:16:09Z #raku <Voldenet> grondilu: by `not working as expected` I meant storing Proxy in an attribute, it's possible to use it if you can accept that accessing the setter always allocates the proxy
grondilu or maybe inherit from Proxy and make the whole class the Proxy? That's what I just tried and failed: 23:36
m: my $p := class :: is Proxy { multi method new { samewith: FETCH => method { pi }, STORE => method ($v) { } } }.new; say $p
camelia ===SORRY!=== Error while compiling <tmp>
Cannot find method 'is_dispatcher' on object of type BOOTCode
at <tmp>:1
Voldenet I'm unsure if it's valid to store a proxy somehow 23:39
grondilu m: my %h; say %h<foo> := Proxy.new: FETCH => method { pi }, STORE => method ($v) { } 23:40
camelia 3.141592653589793
Voldenet Binding operator is crucial, not sure if you can bind to an attribute of an instance
m: class X { has $.p := Proxy.new(FETCH => { 42 }, STORE => method ($n) { say "storing " ~ $n }); } 23:41
camelia ===SORRY!=== Error while compiling <tmp>
Cannot use := to initialize an attribute
at <tmp>:1
------> => method ($n) { say "storing " ~ $n })⏏; }
grondilu well that is a very explicit error message 23:42
Voldenet m: class y { method p is rw { Proxy.new(FETCH => { 42 }, STORE => method ($n) { say "storing " ~ $n }); } }; say y.new.p = 45
camelia storing 45
42
grondilu nice 23:43
Voldenet it sounds like it would allocate a lot
and also
m: class y { method p is rw { Proxy.new(FETCH => { say "fetch"; 42 }, STORE => method ($n) { say "storing " ~ $n }); } }; say y.new.p = 45
camelia storing 45
fetch
fetch
fetch
fetch
fetch
42
Voldenet I didn't benchmark this, but java style getters/setters (explicit methods) sound faster 23:44
grondilu why the many calls to FETCH?
Voldenet …absolutely no idea 23:45
grondilu I could also use FALLBACK with rw trait, I suppose 23:49
m: class { method FALLBACK($str, $value) is rw { say "$value" } }.new.foo("bar") 23:50
camelia bar
grondilu with that I can do anything I want.
Voldenet I like FALLBACK, but I'm expecting serious drawbacks when trying to tool it anyhow 23:52
grondilu m: class X { has $.x; method FALLBACK($str, $value) is rw { Proxy.new: FETCH => method { $!x }, STORE => method ($v) { say "storing!"; $!x = $v } }; my X $x .= new: x => pi; $x.x = 3; 23:53
camelia ===SORRY!=== Error while compiling <tmp>
Missing block
at <tmp>:1
------> } }; my X $x .= new: x => pi; $x.x = 3;⏏<EOL>
grondilu m: class X { has $.x; method FALLBACK($str, $value) is rw { Proxy.new: FETCH => method { $!x }, STORE => method ($v) { say "storing!"; $!x = $v } } }; my X $x .= new: x => pi; $x.x = 3;
camelia Cannot modify an immutable Num (3.141592653589793)
in block <unit> at <tmp> line 1
grondilu wth am I doing? 23:54
Voldenet you're trying to modify constant pi ;)
I'm glad you couldn't
grondilu If I use fallback I don't need a proxy anymore, but I have to process the arg and check it's an attribute name. 23:55
Voldenet m: class X { has $.x is rw; method FALLBACK($str, $value) is rw { Proxy.new: FETCH => method { $!x }, STORE => method ($v) { say "storing!"; $!x = $v } } }; my X $x .= new: x => pi; $x.x = 4; say $x.x
camelia 4
Voldenet m: class X { has $.x is rw; method FALLBACK($str, $value) is rw { Proxy.new: FETCH => method { $!x }, STORE => method ($v) { say "storing!"; $!x = $v } } }; my X $x .= new: x => pi; $x.x = 4; say $x.x; say now - BEGIN { now } 23:56
camelia 4
0.01807157
grondilu the proxy wasn't called
FALLBACK can't be triggered since there is a candidate 23:57
but that's actually what I want for my project
I want to emulate attributes through a hash that contains names of pseudo-attributes 23:58
so FALLBACK will be triggered. 23:59
It might be perfect.