i am a hobbyist, saw and liked Julia and then started reading, and exercising with it. one of the things i like to have in my simple programs is a constant, like avogadro constant, which lacks in python - my favourite toy. i was enthusiastic to read that julia had constants, but my first experiment did not go well.
function nll()
println(“\n\n\n”)
end
const avogadroConstant=6.02e23
println(“avogadroConstant = $avogadroConstant, is a constant\n”)
println(“assign 6.02 to avogadroConstant\n avogadroConstant=6.02”)
avogadroConstant=6.02
println(“\navogadroConstant = $avogadroConstant, is a constant”)
nll()
const avogadroConstant=6.02e23
println(“avogadroConstant = $avogadroConstant, is a constant\n”)
println(“assign 6 to avogadroConstant\n avogadroConstant=6”)
avogadroConstant=6
println(“\navogadroConstant = $avogadroConstant, is a constant”)
nll()
const avogadroConstant=6.02e23
println(“avogadroConstant = $avogadroConstant, is a constant\n”)
println(“assign ‘Julia’ to avogadroConstant\n avogadroConstant=Julia”)
avogadroConstant=“Julia”
println(“\navogadroConstant = $avogadroConstant, is a constant”)
which produced the following:
C:\Users\husband>julia test1.jl
avogadroConstant = 6.02e23, is a constant
assign 6.02 to avogadroConstant
avogadroConstant=6.02
WARNING: redefinition of constant avogadroConstant. This may fail, cause incorrect answers, or produce other errors.
avogadroConstant = 6.02, is a constant
WARNING: redefinition of constant avogadroConstant. This may fail, cause incorrect answers, or produce other errors.
avogadroConstant = 6.02e23, is a constant
assign 6 to avogadroConstant
avogadroConstant=6
ERROR: LoadError: invalid redefinition of constant avogadroConstant
Stacktrace:
[1] top-level scope
@ C:\Users\husband\test1.jl:20
in expression starting at C:\Users\husband\test1.jl:20
assigning “julia” to avogadroConstant also produced an error.
so, in my opinion julia does not have constants like python - though not exactly the same.
assigning the same type of value is ok with a warning.
what is the opinion of you experts out there, does julia have constants or not? shall i have my constant avogadroConstant or not?