For Julia to infer types it needs to know the exact type of the
function. Try this:
type Foo{F<:Function}
f::F
y::Array{Float64, 1}
x::Array{Float64, 2}
end
On Fri, 2016-10-14 at 13:26, Giuseppe Ragusa <[email protected]> wrote:
> I am failing to understand why the following code produce type instability
> (caveat: the code is a reduction o large and more complex code, but the
> features are the same).
>
> ```
> type Foo
> f::Function
> y::Array{Float64, 1}
> x::Array{Float64, 2}
> end
>
> type Bar{T}
> b::T
> end
>
> type A{T}
> a::T
> end
>
> x = randn(100,4)
> y = randn(100)
>
> f(theta, y, x) = x'*(y-x*theta)
>
> (g::Foo)(x) = g.f(x, g.y, g.x)
>
> a = A(Bar(Foo(f, y, x)))
>
> m(a::A) = a.a.b([.1,.1,.1,.1])
> ```
>
> The output of @code_warntype is below:
>
> ```
> @code_warntype m(a)
>
> Variables:
> #self#::#m
> a::A{Bar{Foo}}
>
> Body:
> begin
> SSAValue(1) =
> (Core.getfield)((Core.getfield)(a::A{Bar{Foo}},:a)::Bar{Foo},:b)::Foo
> SSAValue(0) = $(Expr(:invoke, LambdaInfo for vect(::Float64,
> ::Vararg{Float64,N}), :(Base.vect), 0.1, 0.1, 0.1, 0.1))
> return
> ((Core.getfield)(SSAValue(1),:f)::F)(SSAValue(0),(Core.getfield)(SSAValue(1),:y)::Array{Float64,1},(Core.getfield)(SSAValue(1),:x)::Array{Float64,2})::Any
> end::Any
> ```
>
> I thought the v0.5 will solve the problem with functions not being
> correctly inferred. Maybe, I am simply doing something patently stupid.