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. |
|||
01:11
beanbrain joined,
beanbrain left,
beanbrain joined
08:10
dakkar joined
08:12
teatwo left
08:13
teatwo joined
08:20
teatwo left,
teatwo joined
09:03
teatwo left
09:04
teatwo joined
09:44
beanbrain left
11:21
Chanakan left,
Chanakan joined
11:35
discord-raku-bot left,
discord-raku-bot joined
11:36
avuserow_ left
11:37
nicole left
11:44
avuserow_ joined,
nicole joined
12:15
avuserow_ left,
nicole left
12:17
avuserow_ joined,
nicole joined
|
|||
librasteve | @gfldex ... I am smiling since this is a beginner channel that likes to go deep ... and argue the finer points (too late to go back now ;-)) [with apologies to all beginners who are wondering wtf - just ignore our ping pong) | 14:02 | |
so thanks for your patient explanation so far, my interpretation is this: a regular parameter will (a) implicitly "declare" its name in the scope of the block, (b) Bind the name to a thing and (c) store the thing's type in T... | 14:10 | ||
m: sub f(::T $x){say T; say $x}; f(42) | |||
Raku eval | (Int) 42 | ||
librasteve | so, this is analagous to this linear code: | 14:11 | |
m: my Int $x = 42; dd $x; | 14:12 | ||
Raku eval | Int $x = 42 | ||
librasteve | based on that, an optional parameter with an absent argument will (a) implicitly "declare" its name in the scope of the block, errr that's it... | 14:13 | |
m: sub f(::T $x?){say T; say $x}; f() | |||
Raku eval | (Any) (Any) | ||
librasteve | so, this is analagous to this linear code: | ||
m: my Int $x; dd $x; | 14:14 | ||
Raku eval | Int $x = Int | ||
librasteve | oops, sorry, I meant: | ||
my $x; dd $x; | |||
m: my $x; dd $x; | |||
Raku eval | Any $x = Any | ||
librasteve | so far I think that this is a good design - and if I switch $ to @ I get the same analogy | 14:15 | |
m: sub f(::T @a?){say T; say @a}; f() | 14:17 | ||
Raku eval | (Array) [] | ||
librasteve | m: my @a; say @a.^name; say @a; | 14:18 | |
Raku eval | Array [] | ||
librasteve | so I am inclined to think that so far the easy things are easy to do and easy to remember since they are consistent | 14:19 | |
btw in this view, I your is raw example is no different - the raku spec says the implicitly declared / bound internals vars are read-only unless the is raw trait is used so from a coder pov that's a restriction on how the arg is bound ... and the optional form gives a sensible error message in the absence of an arg... | 14:26 | ||
m: sub bar($x? is raw) {dd $x; $x=3}; my $y = 42; bar($y) | 14:27 | ||
Raku eval | Int $y = 42 | ||
librasteve | m: sub bar($x? is raw) {dd $x; $x=3}; bar() | ||
Raku eval | Exit code: 1 Any assign requires a concrete object (got a Any type object instead) in sub bar at main.raku line 1 in block <unit> at main.raku line 1 | ||
librasteve | I think we agree to differ on how best to answer the OP question - for me "don't test an array for defined, test fo Empty (since in raku even Empty Arrays are defined)" is the easy things are (fairly) easy and "you have to use a Capture with a sub-signature and introspect the Capture" is the hard things possible ;-) | 14:30 | |
15:39
teatwo left
15:40
teatwo joined
16:20
sjn left
16:21
sjn joined
16:41
dakkar left
|
|||
gfldex | I don't agree on your analog with my Int $x = 42; dd $x; The whole point of a type-capture is that you don't know the type at compile time but what to do fancy stuff with it at runtime (at least in a Signature, roles are specialised at compile-time). And a type-capture on @a? is outright odd. If no argument is provided by the caller it will be Array, if one is provided it will do Positional. If I would spot | 20:54 | |
that anywhere in the wild, I would start hunting for bugs, right away. | |||
is raw in a Signature allows to get hold of a container the caller is using. So should ::T be Scalar if a container is supplied? | 20:56 | ||
Also, if we have Nil to indicate the absence of a value, why not use it, if no value is supplied? | 20:58 | ||
21:03
teatwo left
21:04
teatwo joined
|
|||
m: sub impossible(Any:D $a?) { } impossible(); | 21:11 | ||
Raku eval | Exit code: 1 Parameter '$a' of routine 'impossible' must be an object instance of type 'Any', not a type object of type 'Any'. Did you forget a '.new'? in sub impossible at main.raku line 1 in block <unit> at main.raku line 2 | ||
gfldex | :-| | ||
ab5tract | IMO this is a bit overboard for a beginner oriented chatroom, but I do agree it's worth discussing | 21:28 | |
that said, I don't understand where the confusion lies with your latest example gfldex | 21:29 | ||
you've declared that the argument needs to be an Any:D, and you didn't provide a defined argument | 21:30 | ||
it's marked optional, of course, but I don't know if I agree that this should supercede the type declaration | 21:31 | ||
maybe it's an LTA though | 21:32 | ||
21:54
lizmat_ joined
21:58
lizmat left
22:00
lizmat_ left
22:01
lizmat joined
|
|||
pelevesque | Guys, sorry I didn't really contribute to the discussion trying to answer my questions. | 22:58 | |
Everyone, sorry I didn't contribute to the discussion on my question. I read everything, but it's a bit over my head. I don't know the Raku internals well enough to contribute. Even some of the code posted as examples go over my head. sub-signatures... I feel like I entered a new galazy. | 23:00 | ||
I think I am more than a beginner. I am a beginner beginning to begin. | 23:02 | ||
antononcube | @pelevesque You can also say: > a sub-beginner who sub-signs in a new sub-galaxy. | 23:14 | |
@pelevesque I think asking the question is enough of a contribution. The discussion is over my head too. | |||
gfldex | I'm a Raku beginner since 2008. :) | 23:19 | |
pelevesque | It would be nice to have some universal way to know if optional args are set... Like $a? @a? \a? or whatever all behaving the same... like a $a.wasSet or something. | ||
gfldex | Sadly, that is not easily done becaue of is raw. We could have a method on `.VAR´ but that requires the container to be created by the binder, not the caller. | 23:21 | |
pelevesque | Seems that if there is a universal way to make an argument optional (the ? char), there should also be a way a universal way to check if it was set. Maybe put in the docs that it can't be done for x reason? | 23:26 | |
gfldex | It can be done, but only with Capture trickery. | 23:27 | |
However, you should not need that because you can just have a multi that does the same in a cleaner way. There might be edge cases, where you can't get around a Capture but I'm not sure if that is in scope of the docs. Feels more like it should be part of the Cook Book (that we don't have right now). | 23:29 | ||
pelevesque | Ah right, so multi would be the right choice for this. Hmmm | 23:30 | |
gfldex | Likely, but as you didn't tell us what you acutally need that for, it's a little tricky to give advice. | ||
As is common with questions that spawn lengthy Raku-discussions. :) | 23:31 | ||
m: sub nice($a? = Nil) { dd $a; } nice(42); nice(); | 23:32 | ||
Raku eval | 42 Nil | ||
gfldex | If you don't want a multi (there can be good reason not to), that might be a nice way. | ||
pelevesque | I have a super ugly sub that I made work in the end. | 23:33 | |
# -------------------------------------------------------------------- sub get_frames ($score-file, $frame-numbers?) { my $grabbing = False; my @frame; my @frames; for $score-file.IO.lines -> $l { my $line = $l.trim; # Ignore empty lines, comments 「-」, and sections 「@」. if $line.chars && $line !~~ /^ <[-@]> / { # Create a new frame and | |||
set grabbing. if $line.starts-with: '#' { if @frame.elems { @frames.push: @frame.clone; @frame = Empty; } if ( ! $frame-numbers.defined || $line.comb(/\d/).join.Int ~~ any @$frame-numbers ) { $grabbing = True; } else { | |||
$grabbing = False; } } elsif $grabbing { # Save a frame's row. my @row = $line.comb; @frame.push: @row; } } } if @frame.elems { @frames.push: @frame.clone; } return @frames; } I was able to use $ for a list... then use @$ to smart match against it, so it worked in the | |||
end. | |||
I'm now have a way better sub than this using only a few lines with a REGEX, so this is abandonned, but it's why the question in the first place. | 23:34 | ||
I wanted undefined to mean all frame-numbers, [] none, and [1, 24, ...| some | 23:35 | ||
gfldex | m: sub nice($a? = *) { given $a { when Whatever { say '*' } when () { say '()' } when .elems > 0 { say 'many' } } } nice(); nice([42]): nice([}); | 23:37 | |
Raku eval | Exit code: 1 ===SORRY!=== Error while compiling /home/glot/main.raku Confused at /home/glot/main.raku:9 ------> nice([42]):⏏<EOL> expecting any of: colon pair | ||
gfldex | Without the typos, that would work. | 23:39 | |
pelevesque | Thanks. I will take note. | 23:40 | |
With $ I can just do .defined though... @a? = * :: gonna test if that would work. | 23:41 | ||
gfldex | m: constant NaA = Array.new; sub nice(@a? = NaA) { say @a =:= NaA } nice(); nice([]); | 23:54 | |
Raku eval | True False | ||
gfldex | That works for @-sigiled bindings. | ||
m: constant NaA = Positional.new; sub nice(@a? = NaA) { say @a =:= NaA } nice(); nice([]); | 23:55 | ||
Raku eval | True False | ||
gfldex | That might be even better. |