Skip to content

Instantly share code, notes, and snippets.

@deoac
Created June 6, 2023 20:04
Show Gist options
  • Select an option

  • Save deoac/784de6ec9b5a2d92f3573058e78e89d2 to your computer and use it in GitHub Desktop.

Select an option

Save deoac/784de6ec9b5a2d92f3573058e78e89d2 to your computer and use it in GitHub Desktop.
Trying to understand the difference between state, our, and my class variables.
#! /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