Unexpected type change - Type declarations


This feature is useful for avoiding performance “gotchas” that could occur if one of the assignments to a variable changed its type unexpectedly.

This “declaration” behavior only occurs in specific contexts:

local x::Int8 # in a local declaration
x::Int8 = 10 # as the left-hand side of an assignment

and applies to the whole current scope, even before the declaration.


Anyone care to expand on these specific contexts?

Valid example for local declaration?

for i=1:10
    local x::Int64
    x = i + functionthatunexpectedlychangestype(i)

Also not clear is the “even before the declaration”


“even before the declaration”: https://docs.julialang.org/en/stable/manual/performance-tips/#type-declarations I’m clipping this to the Types section in my notebook