Are "constants" constant or not

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?

Yes, you should. What is really constant is the type, as you noticed, but that is important for performance.

Also, if you declare it constant, don’t change it* , because functions that have been used (thus compiled) may have assimilated the former value, and do not behave as you would expect after the value is modified.

*Unless you are aware of the consequences

3 Likes

thank you for your time and answer, i feared nobody will give an answer since it is a very simple question, and also because when i first read a comment about julia in the internet (by a pro) he was talking about questions being unanswered in the julia forum (possibly of advanced kind) and its being full of bugs. but i decided to continue on reading about it anyway because its syntax was easy to follow and understand, seemed to me like a neater version of python (i read only the first introductory chapter of “Julia - Bit by Bit” by Kalicharan and did some simple vector (array) calculations to see that it is like numpy available by default). of course, there are tons of things to learn to have a solid opinion, i am just talkng about some superficial syntax things and basic operations, but i can tell that Julia is likable from the stand point of non-programmers.

oh, well, and on my question, i think i can live with a warning, at least it is not going with being unnoticed.

2 Likes

When you get used to Julia, the fact that you can, during development, reassign a constant is sometimes handy. But of course you should not put in production a code that has that warning.

The questions here are pretty much always answered, as far as I notice (maybe on stack overflow they may not get answered? I don’t know). Many, many times, by very experienced Julia developers, the Julia creators, and the most important people in many Julia package fields.

2 Likes

Maybe you want also to check out Introduction · PhysicalConstants ?

1 Like

thank you very much, that is great! but i wish accessing the physical constants was easier.

julia> import PhysicalConstants.CODATA2018
julia> CODATA2018.N_A
Avogadro constant (N_A)
Value = 6.02214076e23 mol^-1

You can do import PhysicalConstants.CODATA2018: N_A as avoconst and then it is referred to as avoconst

7 Likes

Maybe you like to read through the usage section here: GitHub - JuliaPhysics/PhysicalConstants.jl: Collection of fundamental physical constants with uncertainties. It supports arbitrary-precision constants

Especially the syntax

import PhysicalConstants.CODATA2018: c_0, ε_0, μ_0

in your case with N_A, then you can use the constant in any calculation.

This is covered in the manual

https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Constants

And here is a discussion on GitHub explaining the reasoning and issues

2 Likes