Last active
February 27, 2019 01:18
-
-
Save briochemc/b348173c0848217282135ecc67077744 to your computer and use it in GitHub Desktop.
Just a little snippet of code to remind me of the behaviour of constants in Julia
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
const x = 1.0 ; | |
function foo(y) | |
println("x = ", x) | |
end | |
foo("a string") # should print "x = 1.0" | |
# Invoking foo just JIT-compiled foo to use x = 1.0 if the argument is a string. | |
x = 2.0 ; # modify constant | |
foo("another string") # should print "x = 1.0" still | |
# because the argument is still a string, so no more compilation was done | |
# and foo used the previously compiled method for strings that thinks x = 1.0 | |
foo(0) # should print "x = 2.0" this time | |
# because the argument is not a string this time, so a new JIT-compiled foo | |
# was used only this time with the updated x = 2.0 value | |
# Can I define the constants after the function? | |
# Let me change the order... with z undefined at this point | |
function baz(y) | |
println("z = ", z) | |
end | |
baz("yet another string") # should raise an error | |
const z = "baz!" # Define z after the fact | |
baz("yet another string") # Now it should print "baz!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment