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.
pelevesque What is the correct was to check if an optional parameter was passed in a Raku sub. sub test (@a?) { # how to check if @a was passed? } 02:55
04:09 MasterDuke joined
roguerakudev you can use defined @a 04:14
or use a multi sub, e.g. raku multi test(@a) { # @a was passed } multi test() { # @a was not passed } 04:15
04:29 swaggboi left
rcmlz say @a with @a 06:05
say „no“ without @a 06:06
pelevesque I was trying defined, but couldn't get it to work unless it was a scalar. 06:55
I finally ended with this: perl sub d2v (Str $d, $new-map?) is export { state $map = %(< ppp 16 pp 33 p 49 mp 64 mf 80 f 96 ff 112 fff 127 >); $map = $new-map if $new-map.defined; return $map{$d} || Nil } 06:56
Other question: Anyone know why this fails as a type contraint? perl my $rx-note = / <[ABCDEFG]> <[♯♭sf]>? \-? \d+ /; subset Note of Str where * ~~ $rx-note; 06:58
The regex works when used like below, but not when used as above. perl my $rx-note = / <[ABCDEFG]> <[♯♭sf]>? \-? \d+ /; 'G9' ~~ $rx-note ?? say 'Yes' !! say 'No'; 'Ga' ~~ $rx-note ?? say 'Yes' !! say 'No'; 06:59
ab5tract Does subset Note of Str where $rx-note work? 07:02
pelevesque no 07:03
The thing just passed right through
wait 07:06
I have it working now with my original way... mind is blown... it's late I will go to bed and check again tomorrow. Maybe I had another error. Sorry about that.
It fails when in a module. 07:12
It works when I use the constant keyword perl my constant $rx-note = / <[ABCDEFG]> <[♯♭sf]>? \-? \d+ /; subset Note of Str where * ~~ $rx-note; 07:16
07:46 teatime left, teatime joined 08:21 dakkar joined 16:33 dakkar left 19:27 jgaz joined
librasteve m: subset Note where * ~~ / <[ABCDEFG]> <[♯♭sf]>? -? \d+ /; sub fn(Note $n) {say $n }; fn 'G9'; fn 'Ga'; 20:37
Raku eval G9 Exit code: 1 Constraint type check failed in binding to parameter '$n'; expected Note but got Str ("Ga") in sub fn at main.raku line 1 in block <unit> at main.raku line 1
librasteve m: subset Note where * ~~ / <[A..G]> <[♯♭sf]>? -? \d+ /; sub fn(Note $n) {say $n }; fn 'G9'; fn 'Ga';
Raku eval G9 Exit code: 1 Constraint type check failed in binding to parameter '$n'; expected Note but got Str ("Ga") in sub fn at main.raku line 1 in block <unit> at main.raku line 1
librasteve m: subset Note where /<[A..G]> <[♯♭sf]>? -? \d+/; sub fn(Note $n) {say $n }; fn 'G9'; fn 'Ga'; 20:39
Raku eval Exit code: 1 ===SORRY!=== Unrecognized regex metacharacter - (must be quoted to match literally) at /home/glot/main.raku:1 ------> subset Note where /<[A..G]> <[♯♭sf]>?⏏ -? \d+/; sub fn(Note $n) {say $n }; fn Unable to parse regex; couldn't find final '/' at /home/glot/main.raku:1 ------> subset Note where /<[A..G]> <[♯♭sf]>? -⏏? \d+/; sub fn(Note $n) {say $n }; fn 'G
librasteve m: subset Note where * ~~ / <[A..G]> <[♯♭sf]>? -? \d+ /; sub fn(Note $n) {say $n }; fn 'G9'; fn 'Ga'; 20:40
Raku eval Exit code: 1 ===SORRY!=== Unrecognized regex metacharacter - (must be quoted to match literally) at /home/glot/main.raku:1 ------> set Note where * ~~ / <[A..G]> <[♯♭sf]>?⏏ -? \d+ /; sub fn(Note $n) {say $n }; fn Unable to parse regex; couldn't find final '/' at /home/glot/main.raku:1 ------> t Note where * ~~ / <[A..G]> <[♯♭sf]>? -⏏? \d+ /; sub fn(Note $n) {say $n }; fn '
librasteve please ignore the last two (they work in my repl but not on Discord ;-() 20:41
jgaz The docs say `unit` is acceptable as a decorator if the entire program fits within `MAIN`. But what's the point? What is `unit MAIN` telling the interpreter that `sub MAIN` is not? 20:59
errr... `unit sub MAIN`
ab5tract The only difference should be one less pair of curly brackets and an indentation level 21:01
jgaz Oh, I see. Gotcha. 21:02
Thanks
ab5tract Another trade off is that you can’t do multi candidates 21:03
(For MAIN)
But for simple self contained scripts it can be quite nice 21:04
jgaz Good to know! 21:11