šŸ¦‹ Welcome to the MAIN() IRC channel of the Raku Programming Language (raku.org). Log available at irclogs.raku.org/raku/live.html . If you're a beginner, you can also check out the #raku-beginner channel!
Set by lizmat on 6 September 2022.
ab5tract librasteve: I’m a bit surprised to see ā€œreal coders write their own getters/settersā€ 00:00
I have never seen $.syntax described as being for ā€œnot realā€ coders 00:01
(From Reddit, where I lurk but refuse to make an account)
I’ve also never seen a real use case for custom getters/setters in any language, so maybe it’s my blind spot. 00:02
00:04 yewscion left 00:08 librasteve joined 00:16 cm_ joined 00:17 cm left, cm_ is now known as cm
ugexe i feel like i run into real use cases for that quite often, although i can't think of any off the top of my head 00:25
i guess at a minimum for protecting access with a lock
00:55 yewscion joined 01:00 yewscion left
SmokeMachine I used that on Red: github.com/FCO/Red/blob/master/lib...akumod#L87 01:00
01:12 librasteve left 01:13 yewscion joined 01:18 yewscion left 01:24 librasteve joined 01:28 librasteve left 01:38 hulk joined, kylese left 01:50 yewscion joined 01:55 yewscion left 01:56 librasteve joined 02:00 librasteve left 02:09 yewscion joined 02:13 librasteve joined 02:14 yewscion left 02:15 hulk left, kylese joined 02:19 Aedil joined 02:20 librasteve left 02:29 yewscion joined, yewscion_ joined 02:33 yewscion left 02:34 yewscion_ left 02:49 librasteve joined 03:00 xinming left 03:01 xinming joined 03:10 librasteve left 03:24 librasteve joined 03:25 yewscion joined 03:30 yewscion left 03:43 yewscion joined 03:45 kylese left 03:46 yewscion left 03:48 kylese joined 04:02 yewscion joined 04:07 yewscion left
[Coke] 1/win 11 04:24
04:27 librasteve left 04:41 librasteve joined 04:46 librasteve left 05:15 librasteve joined 05:17 yewscion joined 05:21 yewscion left 05:24 librasteve left 05:48 librasteve joined 05:53 yewscion joined 05:55 librasteve left 05:58 yewscion left 06:05 librasteve joined 06:10 librasteve left 06:36 librasteve joined 06:43 librasteve left 06:47 yewscion joined 06:52 yewscion left 06:57 librasteve joined
librasteve oh … perhaps that was a fruity turn of phrase … dials tone down a bit 07:21
07:33 yewscion joined 07:38 yewscion left 07:52 yewscion joined 07:57 yewscion left 08:12 yewscion joined 08:16 yewscion left 08:25 gabiruh_ joined 08:26 guifa_ joined, oodani_ joined 08:27 guifa left, PipStuart left, swaggboi left, camelia left, cm left, kjp left, gabiruh left, ACfromTX left, oodani left 08:28 kjp joined 08:29 cm joined 08:38 swaggboi joined 08:39 PipStuart joined, ACfromTX joined 08:46 camelia joined 08:50 yewscion joined 08:54 yewscion left 09:00 camelia left, ACfromTX left 09:12 ACfromTX joined 09:13 Sgeo left, librasteve left 09:14 librasteve joined 09:19 camelia joined, librasteve left
lizmat ugexe: you mean raku.land/zef:lizmat/Method::Protected ? 09:39
09:44 yewscion joined 09:49 librasteve joined, yewscion left 09:56 dg left, librasteve left 10:04 dg joined 10:06 stanrifkin joined 10:20 yewscion joined 10:24 librasteve joined 10:25 yewscion left 10:29 librasteve left 10:38 yewscion joined 10:39 stanrifkin left 10:43 yewscion left 10:49 librasteve joined 10:54 librasteve left, yewscion joined 10:59 yewscion left 11:01 melezhik joined 11:07 topnep left 11:08 topnep joined 11:25 librasteve joined 11:28 stanrifkin joined 11:30 librasteve left 11:40 [Coke] left 11:42 librasteve joined
wayland tbrowder: My interest in GnuCash was that I'd like to import records from my financial institutions into it. It's a bit further down the list though. 11:49
11:51 yewscion joined
wayland lizmat: The problem (from yesterday) seems repeatable, but I still need to narrow it down. Haven't had time to work on it today, and that may mean a week of delays. Will see how I go. 11:53
11:54 librasteve left 11:56 yewscion left 12:06 librasteve joined 12:09 yewscion joined 12:13 jjido joined 12:14 yewscion left 12:15 librasteve left 12:28 yewscion joined 12:29 librasteve joined 12:32 wayland76 joined, wayland left, yewscion left 12:33 librasteve left 12:47 librasteve joined 12:52 librasteve left 12:57 xinming left, xinming joined 12:59 yewscion joined 13:01 vasko left 13:04 yewscion left 13:05 vasko joined, librasteve joined 13:10 librasteve left 13:12 topnep left, melezhik left 13:13 topnep joined 13:52 librasteve joined 13:57 librasteve left 14:13 Xliff joined
Xliff How can I get the .4 from 3.4 in Raku? 14:13
Is there a method for that or do I have to use ops? 14:14
librasteve modulo? 14:21
14:25 librasteve joined 14:30 librasteve left
Xliff m: 3.4.modulo.say 14:31
camelia No such method 'modulo' for invocant of type 'Rat'
in block <unit> at <tmp> line 1
Xliff m: 3.4.Num.modulo.say
camelia No such method 'modulo' for invocant of type 'Num'
in block <unit> at <tmp> line 1
Xliff *shrug*
14:47 yewscion joined 14:48 Guest76 joined 14:52 yewscion left 14:57 yewscion joined 14:58 librasteve joined 15:01 yewscion left
librasteve m: say 3.4 % 1 15:03
evalable6 0.4
Raku eval 0.4
librasteve % is the Num modulo operator 15:04
timo m: say 55.3.&infix:<mod>(1)
camelia 0.3
timo you can use it as a method at least in terms of syntax
15:07 librasteve left 15:22 Guest76 left 15:30 librasteve joined
Xliff LOL! Works for me, guys! Thanks. 15:32
timo: Here's a question for you. I need to start writing test cases for a project I am working on, and I'd like to use the Test module. However I want to record test output to a buffer, not STDOUT since I will be running the terminal in RAW mode. 15:34
Can I redirect the test routines somehow?
15:36 librasteve left
timo you'll want to set $*OUT and $*ERR for that i'd assume 15:36
Xliff Ah, OK. 15:37
timo haha, oh no 15:38
> # Output should always go to real stdout/stderr, not to any dynamic overrides.
Xliff Where is the code for Test? 15:39
timo rakudo/lib/Test.rakumod
there are subs output and failure_output and todo_output in there that are "is rw"
so what you actually want is not to change $*OUT and $*ERR, but to use these three subs to give it your own handles to output to
Xliff Figured as much. 15:40
This seemed to work, then: use Test; my $o = "out".IO.open( :w ); my $fo = "fail-out".IO.open( :w ); my $to = "todo-out".IO.open( :w ); Test::output() = $o; Test::failure_output = $fo; Test::todo_output = $to; plan 2; ok 1, "IS OK"; nok 0, "IS NOT OK"' 15:45
15:49 yewscion joined 15:50 librasteve joined
timo i'm not sure if there's a need to have multiple different files, or if it's fine to just have them all be the same file 15:53
15:54 yewscion left
Xliff timo: Yeah. This will work for now. Thanks! 16:11
ab5tract ugexe: fair enough! I guess my own position is better ā€œI can’t remember any real use casesā€, though I too know that it’s happened. 16:29
timo in a language where you don't have subset types, or when you want to be able to assign to the attribute from inside the class without imposing the checks that a subset type would bring with it, a setter can be a reasonable spot to put checking logic 16:32
if you have one underlying attribute that is not exactly what you put in, or where there are multiple distinct ways to set and get it, that's where a getter/setter pair (or multiple) can be useful 16:33
if you don't have a corresponding attribute in the first place, but you have to fit some specific API, a setter/getter can be a way to do that
if you have an attribute that's hash-y or array-y and you want to not just take the provided value by updating your instance's reference, but to defensively copy the contents for example, a setter can do that, or if you want to create a "read-only" copy of the array or hash on the way out, that'd be the getter's job 16:35
and for binding libraries through nativecall or something similar, there's often getter and setter functions you have to invoke when something gets changed, so having a Proxy returned from the raku-level class in a getter/setter is the natural fit 16:36
ab5tract Right, I guess I just usually implement those as specific methods (rather than ones that ā€œjustā€ match the attribute name)
But really my favored approach is to be as immutable as possible 16:37
timo aye, often enough it's a matter of taste which you prefer, only sometimes is one clearly better than the other 16:38
ab5tract I appreciate how $.syntax is rw covers a huge subset of general purpose use cases for me personally (especially combined with Subsets) 16:40
refactus one of the uses I've ran into the most was wanting to update some other internal object state when an attribute's value is changed. and, at least back then, proxy incurred a pretty large performance hit
ab5tract Yeah that makes sense. I wonder how our proxy dispatch performance is these days 16:41
timo i believe proxies became better with new-disp, and there's probably more we can do on the spesh level still
in theory in some situations a proxy can become dirt cheap, but we may not have all the necessary optimizations in place yet 16:42
at least some more partial escape analysis work may be necessary, which i have personally not touched at all yet 16:43
so if the frame that calls the accessor that returns the proxy and assigns something into it is fully inlined, i believe the creation of the Proxy object, the taking of the closures, the invocation of the FETCH or STORE sub stored in the proxy object, those could all be eliminated. might be far off, though 16:44
it's a little too warm and humid today to think too much about that stuff :| 16:45
16:54 librasteve left 17:05 librasteve joined
ab5tract :) 17:06
17:07 jjido left
timo a very, very naive benchmark to compare the simplest Proxy with just the default "is rw" public attribute looks like a 7x difference at the moment, but my system is also running other stuff in the background that is interfering with clear measurements so i wouldn't trust that number too much. also, the proxy is so minimal that it's basically 100% overhead for 0% useful work done, i wouldn't call that 17:08
anywhere near apples-to-apples
Voldenet ab5tract: there are a lot of use cases for custom getters/setters - C# has the convention that it uses `string X { get; set; }` which pretty much creates a field and defines get_X and set_X methods by the compiler, newer versions will even let you alter setters/getters partially 17:16
ab5tract What I mean is that I don’t find myself needing them. I certainly don’t think ā€œreal codersā€ use them exclusively 17:17
Voldenet so in the next versions you'll be able to write `string X { get => field ?? throw new Exception("Field is empty"); set; }`
tho you're right – I'd say that real coders autogenerate getter/setters most of the time
ab5tract In Java I hate the convention of getters and setters because I have seen 1% actually do anything custom
The rest are just Lombok annotations for no real reason 17:18
Voldenet actually in java people use lombok which has @Getter and @Setter annotation
ab5tract That’s what I’m saying
Voldenet and there's @Data that even generates getters, setters, tostring etc. 17:19
ab5tract It’s only cultural. Nothing is being added by those annotations except methods that say get/set.
Voldenet conventions are very important part of the language, even if they're not part of the spec 17:20
ab5tract There’s no new guards, no synchronization, it’s just a thing that people do
Voldenet tho in case of getters, java not acknowledging existence of them in spec [citation needed] fully is a mistake 17:21
ab5tract I don’t find ā€œBecause we always do itā€ to be compelling justification when we are talking about programming in the abstract 17:22
Voldenet I really appreciate C# having them built into language, it makes so many things saner
17:23 topnep left
Voldenet like you can ask the reflection "give me setter method for this property" etc. 17:23
ab5tract In Java I do as the Javas do.But I do not consider autogenerated getters and setters to bee ā€œcustomā€ and thus they don’t really fit into what I am saying when I say I don’t know of many genuine use cases for custom getters and setters
17:24 topnep joined
Voldenet yeah, imo you can say it's "custom" when you don't even keep the data in the same form/class 17:24
ab5tract If autogenerated means custom were the case then the whole discussion is pointless because we autogenerated the getters and setters in Raku 17:25
Voldenet and it's sane to do so
ab5tract Sorry, phone doing some past tense
Voldenet I'd say that writing boilerplate is quite contrary to "real programming" 17:26
timo tbf, having 100_000 getters and setters in a project out of which 5 do anything different from the others is a great setup to having your expectations broken :D
Voldenet timo: not necessarily, if a getter and setter, for instance, are only logging accesses to some property, but then return/set data as they should, it's not that bad 17:27
ab5tract Right, I generally want something that’s genuinely custom in this space to have a good name or adverb associated with it that indicates that it’s so
Voldenet I did a mistake of naming a function "get" a few times and
I've always regreted it 17:28
ab5tract It’s all different degrees of pain, really :)
Voldenet even if the interface was "ICookieStorage"
timo "get" means "read and return a single line from the file descriptor" right? :P
Voldenet nonono, get means http GET request, _obviously_ ( ¬‿¬)
ab5tract :) 17:29
timo i'm still kind of unhappy that we have one huge set of objects where .say prints its gist to stdout, and one very small set of objects where .say prints somenthing's gist into it (file handle objects) 17:30
ab5tract Maybe an ā€˜is logged’ trait for attributes would be a fun RakuAST-based meta programming challenge
Voldenet app.get('/', () => "Hello world") # it's quite obvious that this is a getter
timo ISTR a module that makes variables loggable like that, it might also already have something for attributes
ab5tract Interesting! I’ll look around a bit for that 17:31
timo raku.land/cpan:KAIEPI/Trait::Traced
the first example sets "is traced" on a whole class
ab5tract RIP kaiepi :( 17:32
This metaprogramming is less hairy than I expected 17:34
timo indeed, I miss him 17:37
huh. it kind of looks like the section for ROBERTLE in our IN-MEMORIAM document is missing a part. the URL for the obituary over on debian.org/News is also 404ing, but the wayback machine has it. i wonder if there's a reason why we shouldn't copy over the text from the archived page into our document 17:43
Voldenet metaprogramming in raku is amazingly simple, in some languages you'd have to write compiler plugins (or rewrite binary directly, uh) for the same thing 17:48
17:52 jjido joined
librasteve just got back ... wry smile that my crass remarks on Reddit have taken such a turn ... always learn so much from this community! 18:00
I suggest to copy the text over for ROBERTLE ... 18:10
18:17 yewscion joined
Voldenet m: multi trait_mod:<is>(Attribute $a, :$logged!) { $a.package.^add_method($a.name.substr(2), method () is rw { Proxy.new: FETCH => -> $ { my $v = $a.get_value(self); say "GET $a: $v"; $v }, STORE => -> $, $nv { say "SET $a: {$a.get_value(self)} {$a.set_value(self, $nv)}" } }) }; class X { has $.x is logged }; my $n2 = X.new(x=>55); my $n = X.new(x=>42); $n.x = 5; $n2.x = $n.x + 4 18:17
camelia SET $!x: 42 5
GET $!x: 5
GET $!x: 5
SET $!x: 55 9
Voldenet that was a fun challenge
of course, it's a bit crippled, but you can't expect much from one-liner 18:18
it's nice that set value returns the value that's been set, idk if that's actually guaranteed 18:19
18:22 yewscion left
ab5tract I think it is. Not sure if it would make sense to have a conditional return value there. But might be worth checking the source if the docs don’t say it is 18:26
Voldenet still, I'd probably use $nv directly in this
but then I couldn't make it so short
I've tried to use `say "GET $a: {$a.get_value(self).return}"` but it obviously failed 18:27
hmm
m: multi trait_mod:<is>(Attribute $a, :$logged!) { $a.package.^add_method($a.name.substr(2), method () is rw { Proxy.new: FETCH => -> $ { gather say "GET $a: {$a.get_value(self).take}" }, STORE => -> $, $nv { say "SET $a: {$a.get_value(self)} {$a.set_value(self, $nv)}" } }) }; class X { has $.x is logged }; my $n2 = X.new(x=>55); my $n = X.new(x=>42); $n.x = 5; $n2.x = $n.x + 4 18:28
camelia SET $!x: 42 5
GET $!x: 5
SET $!x: 55 5
18:28 librasteve left
Voldenet gather/take works here, but the solution is extremely weird 18:28
i wouldn't expect it to say anything 18:29
and in fact, it does consume one getter for a reason, the code has… risks 18:30
maybe there's some elegance that I'm missing 18:31
18:33 librasteve joined 18:37 yewscion joined 18:41 yewscion left 18:55 librasteve left 19:02 yewscion joined 19:04 Sgeo joined 19:06 yewscion left 19:07 Zedisdef joined 19:13 jjido left 19:19 yewscion joined 19:20 Zedisdef left 19:24 yewscion left, librasteve joined
melezhik. . 19:38
19:39 yewscion joined 19:44 yewscion left 19:56 yewscion joined 20:00 yewscion left 20:02 yewscion joined 20:06 yewscion left 20:33 Aedil left 20:40 yewscion joined 20:45 yewscion left 20:47 librasteve left 20:57 librasteve joined 21:03 yewscion joined 21:08 yewscion left
Voldenet ah right, gather/take would return an iterator in the case above ┐(Ā“ļ½ž`ļ¼›)ā”Œ 21:32
m: (gather say "{42.take}").eager.first.say 21:35
camelia 42
42
Voldenet m: (gather say "{42.take.take.take}").eager.first.say # I wonder how that should work, but it's sufficiently weird 21:39
camelia 42
42
21:49 yewscion joined 21:54 yewscion left 22:20 librasteve left 22:33 librasteve joined 22:38 librasteve left 22:49 librasteve joined 22:57 yewscion joined, librasteve left 23:02 yewscion left 23:10 librasteve joined 23:14 yewscion joined 23:19 yewscion left 23:20 librasteve left 23:33 yewscion joined 23:38 yewscion left 23:43 Sgeo_ joined 23:44 vasko4 joined 23:46 vasko left, vasko4 is now known as vasko, Sgeo left 23:47 librasteve joined 23:52 yewscion joined 23:54 librasteve left 23:56 yewscion left