interesting.  but really i was wondering if there was any way to do so that 
worked within the type system (since presumably the "jit" would need to 
emit new code).

seems not, which i guess isn't surprising.

thanks for the comments, everyone.

andrew


On Sunday, 7 June 2015 14:13:46 UTC-3, Jonathan Malmaud wrote:
>
> I did implement an extremely hacky approach to force the type of an object 
> to change, and it may fail in some situations, but it works well enough:
>
> https://github.com/malmaud/Autoreload.jl/blob/master/src/smart_types.jl#L288
>
> On Sunday, June 7, 2015 at 11:53:03 AM UTC-4, Simon Byrne wrote:
>>
>> 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
>>>
>>>
>>>
>>>

Reply via email to