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.
sampersand—2B +| +^2B == FF how can i raku function return "self"? 09:13
I can use $!foo for instance vars, and $.foo for methods, btu how do you return "self" 09:14
wambash self 09:34
m: class A { has $.b; method s () {self} }; say A.new(:3b).s
Raku eval A.new(b => 3)
uli.pink oh haii sampersand 10:03
im learning raku too
ab5tract To get a reference to self you can do: 15:02
m: class C { has $.a = <a>; method m($THIS: $other-arg = “default value”) { $!a = $other-arg; $THIS } }; my $i = C.new; say $i.m === $i 15:07
camelia True
ab5tract Note that the name and capitalization of $THIS are up to you to choose, nothing is enforced 15:08
sampersand—2B +| +^2B == FF Sick 16:41
ab5tract you can also do type constraints there, as per usual 16:56
m: role R { multi method m(R:U $THIS:) { dd :type-object($THIS) }; multi method m(R:D $THIS:) { dd :defined-object($THIS) } }; R.m; R.new.m;
camelia :type-object(R)
:defined-object(R.new)
ab5tract m: class C { has $.p = "/tmp/foo"; method IO { $!p.IO }; method m(IO() $SELF:) { dd :coerced($SELF) } }; C.new.m 16:58
camelia :coerced(IO::Path.new("/tmp/foo", :SPEC(IO::Spec::Unix), :CWD("/home/camelia")))
ab5tract note that the above can even be shortened via `handles` 16:59
m: class C { has $.p handles <IO> = "/tmp/foo"; method m(IO() $SELF:) { dd :coerced($SELF) } }; C.new.m
camelia :coerced(IO::Path.new("/tmp/foo", :SPEC(IO::Spec::Unix), :CWD("/home/camelia")))
ab5tract and for a final (probably unnecessary) demonstration: 17:10
m: class A { ... }; class B { ... }; role R { multi method m(A:D $S:) { dd :A($S) }; multi method m(B:D $S:) { dd :B($S) } }; class A does R {}; class B does R {}; A.new.m; B.new.m
camelia :A(A.new)
:B(B.new)
thowe I have a csv file that I am reading with a script simply by doing "for $file.IO.lines". some of the fields are missing, and ther eis just no space between the commas like ",,". I am splitting on the commas into an array like so "my @splitline = split(',', $line);". Trying to do anything with the fields which I would expect to just be empty strings or undefined produces errors like "Use of uninitialized value of type Any". How do I check for d 18:22
ab5tract thowe: so I wouldn't expect split to generate any uninitialized values 18:26
thowe the error seems to be generated simply by checking if the value is true? the line generating errors most often is " if ( @splitline[0] ) {" 18:27
if I remove this check, most variations work...
I don't understand something about checking for trueness here 18:28
ab5tract so one thing to consider is that`if @splitline[0]` will not resolve to true if @splitlinep[0] is an empty string
you can check for defined-ness in a number of ways, one of which would be `if @splitline[0] ~~ Str:D` 18:29
this `:D` is a "type smiley" and is a way to say here a defined instance of type `Str` 18:30
thowe yeah, but it isn't just being false, it is generating the error...
OK, lemme try that... 18:31
ab5tract m: say "defined!" if "" ~~ Str:D; say "not defined!" if Str ~~ Str:U; say "not said!" if Str ~~ Str:D;
camelia defined!
not defined!
ab5tract thowe: feel free to share a paste of the larger code if you can/want
thowe That helped... It helped me find another place where I was trying to use something wrong a couple lines later as well. I'm dealing with poorly maintained data. A large part of my task here is going through millions of lines to check if any of these fields are even simply not used at all. It appears to have been constructed from various iterations of a database that they then collate all the data from into a single csv. Some of the data is ei 18:40
I would normally use Perl 5, but I am trying to do more stuff with Raku just because.
ab5tract `--ll-exception` can also come in handy at times like this by (sometimes) pointing towards a more relevant line number 18:45
you can also keep track of line numbers by doing `for $file.IO.lines -> $idx, $line { ... }` 18:48
nahita3882 forgot kv! 18:50
ab5tract oops! lol
thowe should it be $file.IO.lines.kv ? 18:51
yeah, that seems to work better, thanks :) 18:52
ab5tract and it might make sense to leverage `given/when`, `class Missing { has $.line; has $.idx }; my @missing; for split(',', $line).kv -> $field, $value { given $value { when *.chars == 0 { @missing.push: Missing.new: :$line, :$idx } ... }}`
thowe I seem to have solved most of my issues by coercing to string with .gist 18:53
ab5tract sorrhy, that's totally broken
hmm, that sounds like it might be a bit of an anti-pattern, but at the same time: TMTOWTDI! 18:54
.gist is a bit more verbose than .Str (or ~$foo) and a bit less verbose than .raku 18:55
thowe well, it's just Q&D so I can figure out how I want to build the schema I store these in... Basically just detecting unused fields and doing max length checks, etc... 18:56
also, it appears once I fixed a few other assumptions, that I was able to remove a lot of that stuff, fix some line placement, remove a line or two, and fixed the errors anyway. So, yeah, I was creating the issues myself it seems 19:00
ab5tract thowe: here's a non-broken example of what I was attempting above: gist.github.com/ab5tract/ea8ce7d74...c432265d6c 19:01
thowe: nice! definitely not trying to push you in any particular direction, just sharing some random tidbits 19:03
thowe I appreciate it, you are helping 19:05
gitlab.com/-/snippets/4788965 19:07
thowe can you see that? that's an example 19:09
sampersand—2B +| +^2B == FF How do I repeat a list? (1, 2) xx 3yields ((1 2) (1 2) (1 2)), and i want (1 2 1 2 1 2) 19:22
and is !!! or ... preferred 19:23
antononcube You can use .flat. 19:24
Or you can pre-slip: |(1, 2) xx 3 . 19:26
ab5tract thowe: yup I can see it. Looking good :) 20:03