Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

of_eltype does not work for arrays of eltype Any (?) #47

Closed
lassepe opened this issue Apr 29, 2021 · 3 comments · Fixed by #48
Closed

of_eltype does not work for arrays of eltype Any (?) #47

lassepe opened this issue Apr 29, 2021 · 3 comments · Fixed by #48

Comments

@lassepe
Copy link

lassepe commented Apr 29, 2021

julia> import MappedArrays

julia> a = Any[1,2,3]
3-element Vector{Any}:
 1
 2
 3

julia> b = MappedArrays.of_eltype(Int, a)
3-element mappedarray(x->MappedArrays.convert($(Expr(:static_parameter, 1)), x), y->MappedArrays.convert($(Expr(:static_parameter, 1)), y), ::Vector{Any}) with eltype Any:
 1
 2
 3

julia> eltype(b)
Any
@johnnychen94
Copy link
Member

The reason behind this is the type of x->convert(Int, x) is not inferrable:

julia> Base.return_types(x->convert(Int, x), (Any,))
1-element Vector{Any}:
 Any

of_eltype(::Type{T}, data::AbstractArray{S}) where {S,T} = mappedarray(x->convert(T,x), y->convert(S,y), data)

I don't know how to propagate the type information to Base.return_types; a "bad" workaround is:

of_eltype(::Type{T}, data::AbstractArray{S}) where {S, T<:Number} = mappedarray(T, y->convert(S, y), data)

It's bad because it's not very extensible to other cases.

@lassepe
Copy link
Author

lassepe commented Apr 29, 2021

Wow, I was not aware that convert is not inferrable. I would have assumed that this should always be trivially inferrable because it has the return type as an input?

Also, @code_warntype reports:

julia> f = x -> convert(Int, x)
#1 (generic function with 1 method

julia> @code_warntype f(first(Any[1.0]))
Variables
  #self#::Core.Const(var"#1#2"())
  x::Float64

Body::Int64
1%1 = Main.convert(Main.Int, x)::Int64
└──      return %1

Edit: So there it seems inferrable. See answer below.

@cstjean
Copy link

cstjean commented Apr 29, 2021

@code_warntype evaluates the arguments of the function. It's assessing f(1.0)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants