Hi julia developers,
just stumbled upon a limitation in typeinference and minified it to the following example.
The example is actually quite similar to my real usecase, where I like to define a generic map
for multiple Types, supporting extra type-inference.
As you can see, typeinference breaks as soon as the fmap
has multiple overloadings.
struct Iterable{T}
iterable::T
end
a = Iterable(i for i ∈ [1,2,3])
b = Iterable(i for i ∈ [4,5,6])
function fmap end
fmap(f, it::Iterable) = Iterable(f(x) for x in it.iterable)
function nested_eltype()
h = fmap(a) do x
fmap(b) do y
x + y
end
end
# infering eltype of newly created Base.Generator
Core.Compiler.return_type(h.iterable.f, Tuple{eltype(h.iterable.iter)})
end
nested_eltype() # Iterable
function fmap(func, d::Dict)
Dict(k => func(v) for (k, v) in d)
end
nested_eltype() # returns Union{Dict, Iterable}, however should be Iterable
fmap(f, v::Vector) = map(f, v)
nested_eltype() # returns Any, however should be Iterable
EDIT: the expected return type of nested_eltype()
is always Iterable
or something even more concrete
Anyone who knows how to workaround this?
Anyone any idea whether this is improvable in julia?
(Would be awesome if a soon julia version can typeinfer these expressions in detail)