No, you can't change the type of an object. You can however define types with fields that aren't constrained to be of a concrete type: while this is worse for performance, it is occasionally useful, e.g.
type A a::Union(Void, Int) end A(nothing).a = 1 On Saturday, 6 June 2015 23:49:53 UTC+1, andrew cooke wrote: > > > Is there any way to switch the "visible" type - the thing that is > dispatched on - at runtime? > > For example, you might think that a Union() could do this, but the > "visible" type is either always be the Union, and not either of the > subtypes, or doesn't allow the value to be changed. > > julia> type A{T<:Union(Void,Int)} a::T end > > julia> A(1) > A{Int64}(1) > > julia> A(nothing) > A{Void}(nothing) > > julia> A(nothing).a = 1 > ERROR: ... > > julia> A{Union(Void,Int)}(nothing).a = 1 > 1 > > Not sure if those examples make things clear, but when the type of A is > explicitly A{Void} then the contents cannot be changed to an Int. And if > the type is explicitly A{Union(...)} then the value can be changed, but the > "visible" type remains the Union and cannot be dispatched on. > > Thanks, > Andrew > > > >