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 | Been seardching like crazy and can't figure out how to break out of a for loop in raku. | 01:43 | |
antononcube | @pelevesque Still can’t break out? | 02:26 | |
See last. | |||
pelevesque | Hey guys, I'm stumped sub a ($scalar?) { say $scalar.defined } a(1); # True a(); # False This works, but I need it for a list.... sub a (@list?) { say @list.defined } That fails... Is there a way to know if a list as an optional parameter was defined? | 02:55 | |
04:50
beanbrain joined
|
|||
rcmlz | m: sub a (@list?) { say @list with @list; say ”nope” without @list} a((1,)); a() | 06:19 | |
Raku eval | (1) [] | ||
rcmlz | m: sub a (@list?) { say @list if @list.elems > 0; say ”nope” if @list.elems == 0} a((1,)); a() | 06:21 | |
Raku eval | (1) nope | ||
08:33
discord-raku-bot left,
discord-raku-bot joined
09:02
beanbrain left
09:18
discord-raku-bot left
09:19
discord-raku-bot joined
|
|||
gfldex | pelevesque: I take that as Rakudobug, but may not consider all cases. | 12:51 | |
librasteve | gfldex: I understand that .defined Returns False on a type object, and True otherwise. | 12:54 | |
m: ().^name.say | |||
Raku eval | List | ||
librasteve | so an Empty list is defined (which is why the head scratcher, I think) | 12:55 | |
The short answer to the question is: | 13:06 | ||
m: sub a ($list) { if $list ~~ Empty {say 'nope'} else {say $list} }; a((1,)); a(()); | |||
Raku eval | (1) nope | ||
librasteve | Note (i) I use a Scalar $ parameter instead of an Array @ parameter since if the signature of a sub contains one array, then it is treated as a list of positional arguments, but here we want to pass in the Array via a Scalar container to check it as a whole and (ii) there is a misconception in the question of the operation of the ? optional parameter (see below) | 13:11 | |
m: sub a ($list) { if $list ~~ Empty {say 'nope'} else {say $list} }; a((1,)); a(()); a; | |||
Raku eval | Exit code: 1 ===SORRY!=== Error while compiling main.raku Calling a() will never work with declared signature ($list) at main.raku:1 ------> e'} else {say $list} }; a((1,)); a(()); ⏏a; | ||
librasteve | ^^ note I have added a new (parameter-less call to a) ... this fails since $list is a required parameter | 13:12 | |
m: sub a ($list?) { if $list ~~ Empty {say 'nope'} else {say $list} }; a((1,)); a(());a; | |||
Raku eval | (1) nope (Any) | ||
librasteve | with the ? to allow $list to be optional then you can see that raku will compile even if no parameter is passed. (remember that () is an empty List) | 13:14 | |
m: sub a (@list) { if @list ~~ Empty {say 'nope'} else {say @list} }; a((1,)); a(()); | 13:17 | ||
Raku eval | (1) nope | 13:18 | |
librasteve | ^^ without the ?optional marker, using @ does the expected, however | ||
m: sub a (@list?) { if @list ~~ Empty {say 'nope'} else {say @list} }; a((1,)); a(()); a; | 13:19 | ||
Raku eval | (1) nope nope | ||
librasteve | ^^ when you combine ? and @ then the signature makes an empty list [] in an attempt to be helpful (but in this case defeats the check) | 13:20 | |
or, if (like me) you find this test rather hard to understand, you can use raku subset and multi maybe: subset NonEmptyList of List where * !~~ Empty; multi sub a (NonEmptyList $list) { say $list }; multi sub a (List $list) { say 'empty' }; multi sub a { say 'nope' }; a (1,); a (); a; | 13:27 | ||
lizmat | librasteve for the recorded: .defined returns whatever the method returns on that invocant | 15:09 | |
m: say Failure.new.defined | 15:10 | ||
camelia | False | ||
lizmat | is an example of .defined returning False on a non-type object | ||
gfldex | librasteve: I do very well understand what happens here. However, I don't agree that `sub foo(@a?) {}` should be equivalent to `sub bar(*@a) {}`. I do expect that latter to do magic to make the slurpy work. In the case of an optional argument I expect binding, as it is done by the none optional, none slurpy case. | 16:48 | |
gfldex | m: sub a (@list?) { say @list.WHICH } a(); a(); my @b; a(@b); a(@b); | 16:49 | |
Raku eval | Array|5329858285248 Array|5329858296896 Array|5329858296952 Array|5329858296952 | ||
gfldex | As ca be seen, when you supply something to bind to, Rakudo will actually bind. | ||
In my eyes, the optional case should bind to Array, not an Array.new per call. | 16:50 | ||
While riding my bicycle ( $bicycle + $brain > $LLM), I came up with the case I didn't concider. | 16:53 | ||
m: sub foo(@a?, @b?) { say @a =:= @b, @a eqv @b, @a ~~ @b } foo(); | 16:54 | ||
Raku eval | FalseTrueTrue | ||
gfldex | Here it gets … odd, to say the least. How can I compare something that isn't even there? | 16:55 | |
What may be the reason why Larry decided against logic and went for DWIM. | 16:56 | ||
(The „I“ in DWIM is subjective.) | |||
Also, please concider the following. | 17:14 | ||
m: my @b; sub LOLWUT(::T @a?) { dd T }; LOLWUT(@b); | 17:15 | ||
Raku eval | Array | ||
gfldex | In C we got void to express the absense of a type. In Raku, we cannot. We simply can't express the absence of a type, because all Raku objects have a type. | 17:16 | |
What could also mean that Larry was "virtuous" when designing that bit of the language. | 17:18 | ||
m: sub ohhno(::T $p?) { dd T }; ohhno(); | 17:25 | ||
Raku eval | Any | ||
gfldex | I don't agree with that one either. My sprachgefühl wants that to be Mu. | 17:26 | |
17:49
beanbrain joined
18:01
teatwo left
18:02
teatwo joined
18:10
beanbrain left
19:47
beanbrain joined,
beanbrain left,
beanbrain joined
20:43
beanbrain left
22:52
DarthGandalf left
|