I wanted to ensure that my iterative computation preserved type over each iteration of the loop, and found this bug:
function foo(x)
x2 = x
T = typeof(x2)
x2::T = x2 + x2
end
foo(1.0)
causes UndefVarError: T not defined
on 0.6 and 0.7beta2. It seems likely to have been reported already, but I couldn't find it.
So what happens here is that x2::T
is syntax for declaring a variable of that type and causes retroactive insertions of converts/typeasserts. Looks like this is one for @JeffBezanson
This is not expected to work. I think the main thing we could do here is give an error for adding a type declaration to x2
after it has been assigned. That's a breaking change but might make sense in practice.
A separate feature that I think has come up before would be "type-constant variables": a way to declare that a variable's type shouldn't change, though you don't care what it is. It could be useful to come up with syntax for that and implement it, though it's a bit tricky.
It could be useful to come up with syntax for that and implement it, though it's a bit tricky.
Not to start a bikeshed, but what comes to mind immediately is x ::= 1
, which is currently a syntax error and mmenoic for B::typeof(1) = 1
.
Not to start a bikeshed, but
Ha!
Go uses :=
for something similar.
Isn't the const
keyword used precisely for this? To let the compiler know that the type of the variable wont change?
With const
the value also can't change.
julia> const a = 1
1
julia> a = 2
WARNING: redefining constant a
2
WARNING: redefining constant a
Most helpful comment
This is not expected to work. I think the main thing we could do here is give an error for adding a type declaration to
x2
after it has been assigned. That's a breaking change but might make sense in practice.A separate feature that I think has come up before would be "type-constant variables": a way to declare that a variable's type shouldn't change, though you don't care what it is. It could be useful to come up with syntax for that and implement it, though it's a bit tricky.