Reputation: 139
I have a couple of questions regarding Julia. I did some online digging but couldn't find any answer. If I have a file nameA.jl which has a whole lot of variables (for example, physical constants such as a mass of the proton), how can I easily export/call all those variables when I need to use in another file (i.e., nameB.jl).
Second, what is the best way to create some sort of "global" variable through "class" (I know Julia does not have the class concept similar to Python) or another mean, so I can access easily to any other file in the project and modify as I need.
I did try to get these answers from google but couldn't find any help.
Thank you
Upvotes: 5
Views: 1002
Reputation: 1757
To your first question:
PhysicalConstants.jl
module PhysicalConstants
export fine_structure_constant, proton_electron_massratio
const fine_structure_constant = 7.2973525664e-3
const proton_electron_massratio = 1836.15267247
end # module
UsePhysicalConstants.jl
importall PhysicalConstants
this = fine_structure_constant * proton_electron_massratio
# 13.399053416751173
As I understand your second question:
ChangeableValues.jl
module ChangeableValues
export changeable_value, change_value, value
type Changeable{T}
value::T
end
typeof_value{T}(x::Changeable{T}) = T
value{T}(x::Changeable{T}) = x.value
# changeable_value is const for speed
# changeable_value.value is not const
const changeable_value = Changeable(0)
function change_value{T}(new_value::T)
if T == typeof_value(changeable_value)
changeable_value.value = new_value
else
throw(TypeError())
end
return nothing
end
end # module
UseChangeableValue.jl
import ChangeableValues: changeable_value, change_value, value
println("value = ", value(changeable_value)) # 0
change_value(1)
println("value = ", value(changeable_value)) # 1
change_value(2)
println("value = ", value(changeable_value)) # 2
# it remains 2 when imported elsewhere until it is changed again
Upvotes: 3