Last active
July 13, 2023 10:43
-
-
Save librasteve/4229147a003f8e5aafd68458cfc60973 to your computer and use it in GitHub Desktop.
raku my class scoping examples
This file contains 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
CASE 1 | |
------ | |
# this is in file lib/P.rakumod | |
package P { | |
class A is export { has $.a = 42 } | |
} | |
# this is in file test.raku | |
use P; | |
A.new.a.say | |
# run it with | |
> raku -I'./lib' test.raku #42 | |
-or- | |
CASE 2 | |
------ | |
we can simplify the package file so that it only contains classes like this: | |
note that the default is that the class declaration is our scoped so that you do not have to muddy this really clean model with is export traits | |
# this is in file lib/P.rakumod | |
class A { has $.x = 42 } | |
# this is in file test.raku | |
use P; | |
A.new.x.say | |
# run it with | |
> raku -I'./lib' test.raku | |
42 | |
-or- | |
CASE 3 | |
------ | |
we can have multiple classes in the package file | |
# this is in file lib/P.rakumod | |
class A { has $.x = 42 } | |
class B { has $.x = 43 } | |
# this is in file test.raku | |
use P; | |
A.new.x.say | |
B.new.x.say | |
# run it with | |
> raku -I'./lib' test.raku | |
42 | |
43 | |
-and- | |
CASE 4 | |
------ | |
in this case we may want to distinguish between publically available classes that form the API and private classes that | |
are implementation details that we reserve the right to change | |
# this is in file lib/P.rakumod | |
my class X { has $.x = 44 } | |
class A { has $.x = 42 } | |
class B { has $.x = 43 } | |
class C is X { }; | |
# this is in file test.raku | |
use P; | |
A.new.x.say | |
B.new.x.say | |
C.new.x.say | |
X.new.x.say | |
# run it with | |
> raku -I'./lib' test.raku | |
42 | |
43 | |
44 | |
You cannot create an instance of this type (X) | |
in block <unit> at scum.raku line 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment