Reputation: 15406
How to write initialisation for variable that could be nothing in Julia?
id = nothing
title = "Something"
hash = "31114"
id = id || title || hash # Not working
Upvotes: 3
Views: 1047
Reputation: 6976
something
will return the first value that does not equal nothing
. It supports a variable number of arguments:
julia> something(0, nothing)
0
julia> something(nothing, "foo")
"foo"
julia> something(nothing, nothing, 1)
1
You can use it to set the default value for a variable:
x = something(x, DEFAULT_VALUE)
Your example can use:
julia> id = nothing
julia> title = "Something"
"Something"
julia> hash = "31114"
"31114"
julia> id = something(id, title, hash)
"Something"
Upvotes: 8