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:09 MasterDuke left 00:34 MasterDuke joined 00:36 MasterDuke79 joined 00:38 MasterDuke left 00:49 deoac left 01:33 Heptite joined 02:13 teatwo left, teatwo joined 02:40 Heptite left
falsifian Is there a nice concise way to write ($x ?? $x.sqrt !! 'nothing'), or more generally ($x ?? $x.foo !! default)? 03:05
Actually I probably want ($x.defined ?? $x.foo !! default)
(.foo with $x) // $default 03:08
05:06 tea3po joined 05:07 teatwo left 06:53 Manifest0 joined
Nemokosch If you are confident about the interface your variable has when defined, you could go for $x.?foo // $default 07:36
.? means "invoke this method if available, return Nil otherwise"
A more precise variant would be ($x andthen .foo orelse $default); you almost certainly want the parens because these operators have really low precedence 07:38
gfldex falsifian: Raku does not support custom tenary operators (yet) but you can cheat. As with many cheats, you can find how in my blog: gfldex.wordpress.com/?s=tenary 07:46
07:51 dakkar joined 12:45 jgaz joined
falsifian .? is what I was looking for. I used to know about it... thanks Nemokosch. 14:01
gfldex: Good point, I could define my own! 14:02
Hm, how about converting $x into an Int if it's a Match, and otherwise producing just Int? 14:06
14:06 m6502 joined
falsifian m: 'foo' ~~ /foo[$<n>=\d+]?/;$<n>.?Int 14:06
camelia Use of Nil.Int coerced to empty string
in block <unit> at <tmp> line 1
14:06 jgaz left
falsifian m: 'foo' ~~ /foo[$<n>=\d+]?/; $<n> ?? $<n>.Int !! Int 14:06
camelia WARNINGS for <tmp>:
Useless use of constant integer Int in sink context (line 1)
falsifian m: 'foo' ~~ /foo[$<n>=\d+]?/; say( $<n> ?? $<n>.Int !! Int ) 14:07
camelia (Int)
falsifian This came up for me in a script I wrote yesterday.
m: 'foo0' ~~ /foo[$<n>=\d+]?/; say( $<n> ?? $<n>.Int !! Int ) 14:09
camelia 0
14:14 teatwo joined 14:16 teatwo left, teatwo joined 14:17 tea3po left 15:26 Heptite joined
Nemokosch as you can see, tough luck - Nil.Int does exist, at the end of the day... 15:27
and for some reason that is beyond me, it produces an empty string as the result
dakkar github.com/rakudo/rakudo/blob/mast...il.pm6#L37 oh fun! those methods exist to produce a warning, and they all return an empty string 15:41
github.com/rakudo/rakudo/commit/5c...0650cdf3b2 I think it's to make Nil cooperate better with all the multis? 15:44
16:15 theesm left 16:32 dakkar left
lizmat m: dd Nil.Int 16:38
camelia Use of Nil.Int coerced to empty string
in block <unit> at <tmp> line 1
""
lizmat hmmm... that's... unexpected
I think that's just a copy-pasto 16:40
16:41 lonewolf-dev joined
lonewolf-dev hi 16:41
lizmat dakkar: indeed
lonewolf-dev Is raku a compiled language or interpreted? Or just in time compiled like julia and javascript? 16:42
moritz the language spec doesn't care; rakudo currently precompiles modules automatically, and compiles and executes scripts transparently on the fly 17:04
lonewolf-dev ok 17:09
lizmat dakkar Nemokosch falsifian github.com/rakudo/rakudo/commit/70dc3c8a6b 17:19
17:25 lonewolf-dev left 18:17 m6502 left
falsifian lizmat: Out of curiousity, why 0? I think in my particular case I wanted calling .Int on the non-match to produce (Int), because I was storing the extracted parts in the fields of a class and that made sense as a way to represent "no data". 18:46
I don't really know the full context here of where this is likely to be called. 18:47
lizmat well, that may be your expectation, but coercers will never return the type object, as they can only be called on instances
where Nil is a bit of a special case :-)
m: dd Str.Int
camelia Invocant of method 'Int' must be an object instance of type 'Str', not
a type object of type 'Str'. Did you forget a '.new'?
in block <unit> at <tmp> line 1
falsifian lizmat: Thanks, that makes some sense. A couple of follow-up questions... 19:39
Q: Would it also be reasonable to have Nil.Int be not defined or die, rather than warn and return 0? Is it a matter of taste, or is there a strong reason to have Nil.Int actually succeed? 19:40
p6steve nice! 19:41
falsifian Second Q is kind of vague: why can't coercers return the type object? Is that a convention / is it documented / is there something that would clearly break otherwise. I'm trying to tighten up my understanding of how values work in Raku. 19:44
Nemokosch well I like where these questions are going 19:45
if you have my Int(Str) $foo, you can assign Int to it, or any Int instance, or any Str instance. But not Str. 19:50
p6steve just went over to read the Nil page in the docs docs.raku.org/type/Nil 19:51
Nemokosch This doesn't appear to be as algebraic as one would wish
p6steve ^ worth a read (and I guess that .Int will shortly be documented alongside .Numeric) 19:52
falsifian Nemokosch: That's the other direction from my second Q, right? It shows you can't coerce the type object Str to an Int. 19:53
p6steve I'm wondering if need to think about .Real, .Complex, .IntStr and so on also?
falsifian Nil.Reeal and Nil.Complex already warn "Use of Nil in numeric context" 19:55
Nemokosch github.com/Raku/problem-solving/issues/344 19:56
if there is one reason I don't regret any issues that I opened, it's because they make it easier to look stuff up when it comes up in a discussion 19:57
p6steve Tx - I guess that .Numeric is called when coercion hits .Real, .Complex ... should have asked the repl 19:59
I suppose the principle of Nil is "The value Nil may be used to fill a spot where a value would normally go, and in so doing, explicitly indicate that no value is present." [and to do so in a quiet way] 20:04
m: say 1 + Nil
Raku eval 1 Use of Nil in numeric context in block <unit> at main.raku line 1
p6steve m: say 'a' ~ Nil ~ 'b'
Raku eval ab Use of Nil in string context in block <unit> at main.raku line 1
Nemokosch m: say 1 + Empty; say 'a' ~ Empty ~ 'b'; 20:05
Raku eval 1 ab
falsifian One difference between Int and Real/Numeric is the latter are roles.
re issue 344
Nemokosch however, the calls were all made on Int anyway 20:06
I think the whole situation around missing/undefined/faulty values gives worryingly much ammunition for jokes...
p6steve ^^ ah 20:07
m: say 1 + [1] 20:09
Raku eval 2
Nemokosch because it has one element
m: say 1 + [99, 110]
Raku eval 3
p6steve ha!
m: Empty.Int 20:10
Raku eval
Nemokosch because the bot doesn't &say by default 😛
p6steve m: say Empty.Int
Raku eval 0
p6steve always gets me 20:11
Nemokosch if there is something I learned, it's that you can chase down a whole lot of behavior in the sources simply by using CoreHackers::Sourcery and a handful of introspection (meta)methods 20:12
if you know that the + operator corresponds to the &infix:<+> subroutine (a usual structure for operators), you can install CoreHackers::Sourcery and then do 20:14
use CoreHackers::Sourcery; &infix:<+>.sourcery: 1, [99, 100].say;
aalmoost, I didn't nail the precedence for the second line 20:15
say &infix:<+>.sourcery: 1, [99, 100]
perhaps simpler
then you get the file name and the line, and a clickable link in the rakudo github repository
github.com/rakudo/rakudo/blob/2022...c.pm6#L211 20:16
to figure out the array, we can use the other way to invoke it: say sourcery [99, 100], 'Numeric'; 20:17
github.com/rakudo/rakudo/blob/2022...t.pm6#L366 and dang, we can immediately say it's defined using the .elems method!
p6steve I try to stay in pure raku userland and avoid digging in the nqp compiler sources ... asking the repl usually works for me 20:20
Nemokosch well I don't know a Raku way to tell you what a certain function or method does 🙂 20:23
or better said: I know one but that got gradually lost over the years - the core library used to be written in Raku, or as it was called then, Perl 6 20:24
22:34 Heptite left 22:59 Manifest0 left