Le vendredi 22 avril 2016 à 09:15 -0400, Yichao Yu a écrit :
> On Fri, Apr 22, 2016 at 9:02 AM, Milan Bouchet-Valat  wrote:
> > 
> > Hi! Yet more explorations regarding inference and varargs/tuple
> > arguments.
> > 
> > I have a function taking a tuple of vectors, and I need a way to
> > extract the element types of each of them, in order to create a Dict.
> > The following code works fine, but inference is not able to compute T
> > at compile time:
> > function f(x::Tuple)
> >     T = Tuple{map(eltype, x)...}
> >     Dict{T,Int}()
> > end
> > 
> > @code_warntype f(([1,2], [1.,2.],))
> > 
> > The same happens with varargs, i.e. f(x...).
> > 
> > Is there any solution to this (other than generated functions)? Am I
> > missing an existing bug report again?
> This is the case `@pure` is replacing `@generated` on 0.5-dev (More
> flexible type computation without non-trivial code generation)
> 
> ```
> julia> Base.@pure f(x) = Tuple{map(eltype, x.parameters)...}
> f (generic function with 1 method)
> 
> julia> function g(x::Tuple)
>            Dict{f(typeof(x)),Int}()
>        end
> g (generic function with 1 method)
> 
> julia> @code_warntype g(([1, 2], [1., 2.]))
> Variables:
>   #self#::#g
>   x::Tuple{Array{Int64,1},Array{Float64,1}}
> 
> Body:
>   begin  # REPL[2], line 2:
>       return 
> (Dict{Tuple{Int64,Float64},Int64})()::Dict{Tuple{Int64,Float64},Int64}
>   end::Dict{Tuple{Int64,Float64},Int64}
> ```
Great, thanks! I could have thought about this myself. Should I file an
issue about achieving this optimization automatically, though?


While I'm at it, I'm having problems with the next step:
function f(x::Tuple)
    for (i, el) in enumerate(zip(x...))
        @show i, el
    end
end

@code_warntype f((1:2,))

Where does Union{Int64,Tuple{Int64}} come from? I've fixed zip() some
time ago to ensure it always returns a tuple...


Regards

Reply via email to