Skip to content

getfield broadcast on empty collection doesn't infer #25691

Open

Description

On Version 0.7.0-DEV.3424, Commit 6afcb045b5 (and also on 0.6)

struct A
  b::Int
  c::Float64
end

As = [A(0,0) for i=1:10]

for idxs in [[1], []]
  println(ifelse(length(idxs)==0, "empty", "full"))
  @show eltype([getfield(a, :b) for a in As[idxs]]) # same as  eltype([a.b for a in As[idxs]])
  @show eltype([getfield(a, :c) for a in As[idxs]]) # same as  eltype([a.c for a in As[idxs]])
  @show eltype(getfield.(As[idxs], :b))
  @show eltype(getfield.(As[idxs], :c))
end

produces

full
eltype([getfield(a, :b) for a = As[idxs]]) = Int64
eltype([getfield(a, :c) for a = As[idxs]]) = Float64
eltype(getfield.(As[idxs], :b)) = Int64
eltype(getfield.(As[idxs], :c)) = Float64
empty
eltype([getfield(a, :b) for a = As[idxs]]) = Int64
eltype([getfield(a, :c) for a = As[idxs]]) = Float64
eltype(getfield.(As[idxs], :b)) = Union{Float64, Int64}
eltype(getfield.(As[idxs], :c)) = Union{Float64, Int64}

(the Union is unexpected)
I guess inference still works on getfield, even though it's type-unstable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions