This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
00:17 hythm joined 00:31 Manifest0 left 01:41 hythm left
lucs m: class Baz { has $.foo }; my $baz = Baz.new: foo => sub { say 'Baz' }; $baz.foo() 01:59
camelia ( no output )
lucs How do I make something like that work?
kjp lucs: You mean something like... 02:23
m: class Baz { has $.foo; }; my $b = Baz.new(foo => 42); say $b.foo;
camelia 42
kjp The declarator "has $.foo" automatically creates a getter method.
If you need a setter, then "has $.foo is rw" will create that as well. 02:24
lucs What I'm actually after is a way to have the 'foo' attribute be a subroutine that can be set by the constructor, then run by the instance. 02:26
kjp Okay, so something like “Class Baz { has $!foo; }; my $baz = Baz.new(foo => sub {...});” and then code in other methods could call “$!foo(...)”. 02:43
But I doubt you'd get direct access to other attributes. They'd need to be arguments to foo.
lucs Feels closer, yet: 02:49
m: class Baz { has $!foo; method jub { $!foo } }; my $baz = Baz.new: foo => sub { say 'Baz' }; $baz.jub;
camelia ( no output )
kjp You'll need to call the function. You need the parentheses. “$!foo()”. 02:50
lucs m: class Baz { has $!foo; method jub { $!foo() } }; my $baz = Baz.new: foo => sub { say 'Baz' }; $baz.jub(); 02:51
camelia No such method 'CALL-ME' for invocant of type 'Any'
in method jub at <tmp> line 1
in block <unit> at <tmp> line 1
kjp m: class Baz { has $!foo is built; method jub { $!foo() } }; my $baz = Baz.new: foo => sub { say 'Baz' }; $baz.jub() 03:11
camelia Baz
kjp Got there!
lucs Nice! 03:15
lucs reads up on 'is built' 03:16
Oh, right, I should have known that. 03:17
kjp So should I!
lucs Thanks for working it out. 03:18
kjp No problems. Maybe next time I won't have to think as long :-) 03:19
Dr.Doom how to apply f() on all the values in %hash 05:13
Nahita with $baz.foo or equivalently $baz.foo() you are accessing the attribute, i.e., the subroutine itself, via the automatically made public getter method. You need to call that subroutine further via $baz.foo.() or $baz.foo()(). Noting that in the first one we need a . otherwise it's again $baz.foo() 05:58
This is mentioned in the second paragraph with an example matching yours here: docs.raku.org/language/operators#m...tcircumfix
07:22 Manifest0 joined 09:06 guifa left 09:07 guifa joined
ab5tract_ Dr.Doom: docs.raku.org/language/hashmap#Loo...and_values 09:09
Though it’s kind of strange that we don’t provide a visit method, to be honest 09:10
10:42 Manifest0 left
lizmat And yet another Rakudo Weekly News hits the Net: rakudoweekly.blog/2023/10/30/2023-...ngly-good/ 12:00
12:26 teatime joined, teatime left 13:23 jgaz joined 14:09 raiph joined 16:21 raiph left 17:33 habere-et-disper joined
habere-et-disper How do I pass a parameter to a method of an action class (for a raku grammar) ? Using `:args` we can send it to the grammar but I want to send it to the action class. 17:36
nemokosch well, what does the call look like? 17:42
librasteve good question - I would say that the only reliable way to do that is to pass any arg values through the match object using make and made (which takes some study and getting used to), here's an example (taken a little out of context): 17:44
##use Grammar::Tracer; grammar UnitGrammar { token TOP { ^ \s* <numerator=.compound> [\s* <divider> \s* <denominator=.compound>]? [\s* '+' \s* <offset> ]? \s* $ } #offset '+' hardwired token divider { '/' || 'per' } token compound { <element>+ % <sep> } token sep { [ '*' || '.' || ' *' || 17:45
' .' || ' ' || '⋅' ] } token element { <factor> || <pnp-before> || <pnp-after> } ... #| accumulates element Units using times method compound($/) { my $acc = Unit.new; for $<element>>>.made -> $x { $acc.times($x); } make $acc; }
nemokosch Raku's grammar (as per Rakudo) uses tons of dynamic vars for sharing state 17:49
librasteve so, roughly what is going on is when I match token element, I use make in the method element($/) action to stash the value of my arg (could be a List or Hash if you have multiple args) and then when I match token compound, I can use $<element>.made (in this case there can be multiple elements in a List) to recover the arg value(s)
I have fallen foul of any other mechanisms because of the quasi asynchronous way that matches are made ... ie you dont know what is going to match in what order 17:50
I guess dynamic vars (or lexicals in the outer scope) can be used to pass info around ... but it has always totally confused me when I try this due to the unpredictability 17:52
docs.raku.org/language/grammar_tut...e_and_made 17:53
habere-et-disper Thanks ! I want to pad line numbers but only once the last line is successfully parsed by the grammar will I know what the largest/final line number is and so be able to calculate the padding for the action class -- which I think is run piecemeal. So I'm unsure how to delay the action or if that's necessary. I'm reading the file twice as a 18:08
workaround which seems suboptimal.
librasteve oh - so you mean 999 needs more pad than 99 even on line 1? 18:11
habere-et-disper Yes -- right aligned 18:12
librasteve since your don't know until the last line, maybe it would be better to post process the lines after the grammar is done 18:14
habere-et-disper That's what I thought `actions` are for -- but perhaps not in this case? 18:16
librasteve the action for a token match is called when the token is matched. how would the action for line 48 know that the total lines is 480 and not 98? 18:20
habere-et-disper Maybe there is a final action, or a `TWEAK` ?
librasteve you may be able to try something like: method line($/) { make %(text => $parsedline, number => $linenumber } method TOP($/) { my @texts = [$<line><text>>>.made]; my @nums = [$<line><number>>>.made]; for ^@nums -> $i { say @num[$i] ~ pad($i, +@nums) ~ @text[$i] } } 18:27
^^ your sub pad( $i, +@nums) {...} can then be right sized 18:28
here TOP is the final match made so method TOP($/) {...} is the final action where you can do clean up
[you may have to play around with the dereferencing - its been a while since i wrote one of these in anger 18:29
]
habere-et-disper Thanks Steve and for the laugh ! :') 18:34
22:01 deadmarshal_ left 22:18 hythm joined 23:13 Dominika joined 23:15 sdomi left, thowe left, jgaz left, jgaz joined 23:20 thowe joined 23:40 lizmat left, lizmat joined