I was coding something today, and I have a variable V which can be of many types (Int, Float32, Float64, and so on). However, I need to limit the value of this variable. What I did was:
V > +10 && (V = eltype(V)(+10))
V < -10 && (V = eltype(V)(-10))
However I found this strange… So, I would like to propose the indrocution of broadcast operator for such primitive types, so that we can do:
V .= 10
and V will not change type. Of course, if conversion is not possible, then an error will be thrown.
What do you think?
EDIT: I know we have the sign function that would do what I need, but I still think this will be nice to have.
It is not “broadcast”, I do not know how to call it… but an operator that will keep the type of the variable on the left side if it is a scalar. Doesn’t make sense?
Yes, that will be much better than my previous approach.
This is a little pedantic, but Julia’s identifiers aren’t really “variables.” They’re just names. So the only way to “modify” the name is to completely re-purpose the same name to identify something different.
Using .= and thinking about “modifying” a variable leads your mental model down a path that points towards more confusion, I think.
What you can do is declare that this name will always identify something with a certain type with the V::T = ... syntax. You could do this as a macro, too, with a @typeconst V = rhs expanding to V::typeof(rhs) = rhs.
But to your actual original example: just use clamp
julia> V = 8.0
8.0
julia> clamp(V, -10, 10)
8.0
julia> V = 22.5
22.5
julia> clamp(V, -10, 10)
10.0
I would need to compute T, which in that case I did not have. So there would be a T = eltype(V) somewhere.
Fair enough!
I really liked this idea I will try to put in a package.
Indeed! @mcabbott told me about this function in Slack. I did not know how I missed that. You have no idea how many times I needed to do such a thing living and learning!