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

Improve type inference for @something #52381

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,31 @@ true
This macro is available as of Julia 1.7.
"""
macro something(args...)
expr = :(nothing)
noth = GlobalRef(Base, :nothing)
something = GlobalRef(Base, :something)
Seelengrab marked this conversation as resolved.
Show resolved Hide resolved

# This preserves existing semantics of throwing on `nothing`
expr = :($something($noth))

#=
We go through the arguments in reverse
because we're building a nested if/else
expression from the inside out.
The innermost thing to check is the last argument,
which is why we need the last argument first
when building the final expression.
=#
for arg in reverse(args)
expr = :(val = $(esc(arg)); val !== nothing ? val : ($expr))
val = gensym()
expr = quote
$val = $(esc(arg))
if !isnothing($val)
# unwrap eagerly to help type inference
$something($val)
else
$expr
end
end
end
something = GlobalRef(Base, :something)
return :($something($expr))
return expr
end