I like how in python I can set a variable in a function as None if I don’t always need to use it. And if the function does end up being called with that variable passed in I can check in the function body by doing:
id = id or str(uuid.uuid4())[0:8]
In Julia however I do:
if id === nothing
id = string(UUIDs.uuid4())[1:8]
else
id = id
end
That seems overly defensive, there is definitely no chance that will change within 1.x. Using === is actually preferable prior to Julia 1.7, because it let the compiler reason better about type constraints.
That’s a reasonable question. It’s mostly useful for higher-order functions like in filter(isnothing, x), but now that we have ==(nothing), it might not be that useful anymore.
The usual python x = x or something / if x is None idiom is an ugly workaround existing only because the default argument for a function is a value in Python (while Julia inherits and improves upon Lisp behavior where a default written as a function call is an expression).
Now just use check_id wherever you want. If id is nothing it returns nothing. If id is anything else it returns your UUIDs construct. This will run really fast and is type-stable.
The condition ? (x = x) : (x = y) construct on the other hand may result in the compiler being unable to infer the type of x until run-time which depending on how your code is structured could really slow things down.