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:15 jgaz left, jgaz joined 02:21 teatwo joined 02:25 tea3po left 02:28 deadmarshal_ left 03:40 deadmarshal_ joined 04:05 deadmarshal_ left 05:35 deadmarshal_ joined 08:11 dakkar joined 09:08 lizmat left 09:09 lizmat joined 09:55 deadmarshal_ left 11:08 deadmarshal_ joined 11:22 human-blip left, human-blip joined 13:32 habere-et-disper joined 13:34 jgaz left, jgaz joined 13:44 habere-et-disper left 14:20 teatwo left 14:22 teatime joined 16:42 dakkar left 17:25 deoac joined
deoac In a `class` , what's the difference between `state $i` and `our $i` ? 18:28
librasteve a class is just a fancy block 19:17
my scope lexical variables are available in the block and in any enclosed blocks 19:18
our scope package variables are available anywhere in the package and not limited to the block ... this is generally bad since you lose locality 19:19
my scope variables are re-declared each time their block is entered - so generally they are different in each pass (for example in recursive code, each call to the block has a new set of my variables) 19:21
state scope variables are local to the block, but they are only initialized once, so state variables will retain their value across multiple executions of the enclosing block or routine 19:24
so you can use them for things like count how many times a block is executed 19:25
in a class this means you can use a class state variable to count (or limit) the number of instance
Nemokosch a class itself is a package, let's not forget that
deoac I can also use an our variable to count how many times the class in initiated. 19:32
librasteve m: class A { state $s; method s { $s += 1 } } my $a1 = A.new; say $a1.s; my $a2 = A.new; say $a2.s; 19:35
Raku eval Exit code: 1 ===SORRY!=== Error while compiling /home/glot/main.raku Strange text after block (missing semicolon or comma?) at /home/glot/main.raku:1 ------> ass A { state $s; method s { $s += 1 } }⏏ my $a1 = A.new; say $a1.s; my $a2 = A.n expecting any of: infix infix stopper statement end statement modifier statement modifier loop
librasteve m: class A { state $s; method s { $s += 1 } }; my $a1 = A.new; say $a1.s; my $a2 = A.new; say $a2.s;
Raku eval 1 2 19:36
librasteve m: class B { our $o; method o { $o += 1 } }; my $b1 = B.new; say $b1.o; my $b2 = B.new; say $b2.o;
Raku eval 1 2
librasteve I had not appreciated that class is a package, so both our and state are available to all instances (ie class variables) 19:37
m: class A { state $s; method s { $s += 1 } }; my $a1 = A.new; say $a1.s; say $A::s; 19:46
Raku eval 1 (Any)
librasteve ^^^ state variables (like my vars) are fully private 19:47
m: class B { our $o; method o { $o += 1 } }; my $b1 = B.new; say $b1.o; say $B::o;
Raku eval 1 1
librasteve ^^^ you can access the our variable (eg from another class) via the fully qualified name (fqn) 19:48
I guess you already saw the Class Variables doc docs.raku.org/language/classtut#Class_variables
looking at these examples, the class block is only initialized once so there is no special behaviour of state vs my afaict since the magic of state is that it is only initialized once when the block is executed multiple times 19:56
deoac Is this summation accurate?  gist.github.com/deoac/784de6ec9b5a...e89d2.html 20:06
librasteve there is a small point 'my class A' is fine but 'class A' does the same thing since class is default 'my scoped' 21:04
otherwise, at a quick look it is right and I see that you have added TWEAK as the point to do the increment which looks good 21:05
BUT --- I think that your code example clarifies that (i) using state scope for a class variable is useless (and thus may be confusing to the reader) since the class block is only executed once and (ii) there is only a very fine distinction between my scope and our scope in that our scope is slightly leaky since you can use FQN and, unlike my is not strictly private 21:09
oh - I think that there is an error in your gist in that dd $B::count should give you the value 21:11
^^ no your gist is right, and it also shows that inheritance via 'is' does not clone the FQNs to the child class name ... which seems fine to me as default behaviour 21:22
deoac Thanks for all this help.  Can you give me an example where a state variable would behave differently from an our variable? 21:32
avuserow m: sub foo {state $s = 0; $s++; say $s}; foo() for ^5 22:43
camelia 1
2
3
4
5
avuserow m: sub foo {our $s = 0; $s++; say $s}; foo() for ^5
camelia 1
1
1
1
1
avuserow m: {my $s = 0; sub foo {$s++; say $s}}; foo() for ^5 # approximation of `state` using another block 22:44
camelia ===SORRY!=== Error while compiling <tmp>
Undeclared routine:
foo used at line 1
avuserow m: {my $s = 0; our sub foo {$s++; say $s}}; foo() for ^5 # approximation of `state` using another block
camelia ===SORRY!=== Error while compiling <tmp>
Undeclared routine:
foo used at line 1
avuserow m: my &foo = do {my $s = 0; sub {$s++; say $s}}; foo() for ^5 # approximation of `state` using another block 22:48
camelia 1
2
3
4
5