Created
June 6, 2023 20:04
-
-
Save deoac/784de6ec9b5a2d92f3573058e78e89d2 to your computer and use it in GitHub Desktop.
Trying to understand the difference between state, our, and my class variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env raku | |
| use v6.*; | |
| multi MAIN ( ) { | |
| my class A { | |
| state $count = 0; say "Using a state variable"; | |
| # our $count = 0; say "Using an our variable"; | |
| # my $count = 0; say "Using a my variable"; | |
| submethod TWEAK { $count++; } | |
| method count { "$count instances"; } | |
| } | |
| my class B is A { ; } | |
| # output when using 'our' | output when using 'state' | output when using 'my' | |
| # ------------------------------------------------------- | | |
| my $a = A.new; | | | |
| say $a.count; # 1 instances | 1 instances | 1 instances | |
| dd $A::count; # Int $count = 1 | Any element{'count'} = Any | Any element{'count'} = Any | |
| | | | |
| my $b = B.new; | | | |
| say $b.count; # 2 instances | 2 instances | 2 instances | |
| dd $B::count; # Any element{'count'} = Any | Any element{'count'} = Any | Any element{'count'} = Any | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment