Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constants can be lexically scoped in Raku #220

Open
gitjeff2 opened this issue Apr 14, 2021 · 0 comments
Open

Constants can be lexically scoped in Raku #220

gitjeff2 opened this issue Apr 14, 2021 · 0 comments

Comments

@gitjeff2
Copy link

In ncitest.raku a constant is defined like this:

constant LIBPATH = "$*CWD/ncitest";

That's entirely valid. However, it really is worth noting to the reader that in Raku, unlike Perl, constants can be lexically scoped with my just like a variable. Thus, the following works as you'd expect:

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

The result is:

$ ./constant.raku 
foo
bar

Inside of scopes where $A is already defined as a constant attempts to redefine $A will produce an error. It's worth mentioning this near ncitest.raku, where the first use of constants occurs in the guide. Given the importance of read-only variables to Functional Programming, it's worth reiterating in that section too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant