Re: [julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Stefan Karpinski
The key behavioral difference between these two is that with the non-parametric union version you can change both the value and the type of the field of a MyType object after construction, whereas in the parametric version you can change the value of the field after an object has been constructed,

Re: [julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Cedric St-Jean
On Wednesday, October 28, 2015 at 8:10:41 AM UTC-4, Stefan Karpinski wrote: > > The key behavioral difference between these two is that with the > non-parametric union version you can change both the value and the type of > the field of a MyType object after construction, whereas in the

Re: [julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Tomas Lycken
Yeah, that's a good point. When you give the compiler the assurance that "I'm not going to change the type of x here", you also loose the possibility to do that :) Type instabilities are a major performance bottleneck (one of the most common performance problems, actually...) but in many

[julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Tomas Lycken
With a type declared like that, any access of the field x will be type unstable, which means that Julia’s JIT compiler will emit much more defensive, and thus slower, code. The solution is to declare a *parametric* type: type MyType{T<:Union{Int64, Float64}} x::T end That way, MyType(3)

[julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Gnimuc Key
that makes sense. many thanks! 在 2015年10月28日星期三 UTC+8下午3:14:26,Tomas Lycken写道: > > With a type declared like that, any access of the field x will be type > unstable, which means that Julia’s JIT compiler will emit much more > defensive, and thus slower, code. > > The solution is to declare a

Re: [julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Jonathan Malmaud
One reason would be to reduce pressure on the compiler: this will perform badly in terms of memory usage and compilation time because a separate version of "f" has to be compiled for every "T": immutable X{T} val::T end f(x) = println("got $(x.val)") for i=1:10^5 f(X((zeros(i)...))) end