ash / raku-course

28 stars 11 forks source link

Constants Should Be Covered in Raku Essentials #17

Open gitjeff2 opened 3 years ago

gitjeff2 commented 3 years ago

Unless I missed something, constants aren't covered at all in the course. Constants are worthy of mention, most likely in the Variables and Data Types Essentials section. It should be noted that in Raku, unlike Perl, constants can be lexically scoped with my just like a variable.

Thus, this code works as expected:

my constant $A="bar";    

sub foo {    
    my constant $A="foo";    
    say $A;    
}    

foo();    
say $A;    

The user can expect the following output:

$ ./constant.raku 
foo
bar

Attempts to redefine a constant will throw an error. Given the importance of read-only variables in FP, this is all worth revisiting in Course 5: Functional, Concurrent, and Reactive Programming.