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

inference: if it doesn't know how to quack, it can't be a duck #37866

Open
KristofferC opened this issue Oct 3, 2020 · 0 comments · May be fixed by #40880
Open

inference: if it doesn't know how to quack, it can't be a duck #37866

KristofferC opened this issue Oct 3, 2020 · 0 comments · May be fixed by #40880
Labels
compiler:inference Type inference

Comments

@KristofferC
Copy link
Sponsor Member

KristofferC commented Oct 3, 2020

It would be cool if inference could figure out that if you call a method on an object inferred as a union, inference could ignore all types that are known to throw with that method for future expressions in that block. For example:

function find_first_above_5(v::Vector{Union{Nothing, Float64}})
    for x in v
        if x > 5.0
            return x # x > 5.0 is MethodError for Nothing so can assume ::Float64
        end
    end
    return 0.0
end

x = Union{Nothing, Float64}[1.0, 2.0, 3.0, 6.0, 4.0]
find_first_above_5(x)
# 6.0

Core.Compiler.return_types(find_first_above_5, Tuple{Vector{Union{Nothing, Float64}}})
# 1-element Array{Any,1}:
#  Union{Nothing, Float64}

code_warntype(find_first_above_5, Tuple{Vector{Union{Nothing, Float64}}})

Here, a return value of Float64 could be inferred.

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

Successfully merging a pull request may close this issue.

1 participant