Skip to content

Commit 9bbac5a

Browse files
c42fLilithHafner
authored andcommitted
Make Base.ifelse a generic function (JuliaLang#37343)
Allow user code to directly extend `Base.ifelse` rather than needing a special package for it.
1 parent 0cd3092 commit 9bbac5a

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

base/boot.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ export
192192
Expr, QuoteNode, LineNumberNode, GlobalRef,
193193
# object model functions
194194
fieldtype, getfield, setfield!, swapfield!, modifyfield!, replacefield!,
195-
nfields, throw, tuple, ===, isdefined, eval, ifelse,
196-
# sizeof # not exported, to avoid conflicting with Base.sizeof
195+
nfields, throw, tuple, ===, isdefined, eval,
196+
# ifelse, sizeof # not exported, to avoid conflicting with Base
197197
# type reflection
198198
<:, typeof, isa, typeassert,
199199
# method reflection

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, (; fargs
10821082
sv::InferenceState, max_methods::Int)
10831083
@nospecialize f
10841084
la = length(argtypes)
1085-
if f === ifelse && fargs isa Vector{Any} && la == 4
1085+
if f === Core.ifelse && fargs isa Vector{Any} && la == 4
10861086
cnd = argtypes[2]
10871087
if isa(cnd, Conditional)
10881088
newcnd = widenconditional(cnd)

base/compiler/optimize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const _PURE_BUILTINS = Any[tuple, svec, ===, typeof, nfields]
162162
const _PURE_OR_ERROR_BUILTINS = [
163163
fieldtype, apply_type, isa, UnionAll,
164164
getfield, arrayref, const_arrayref, isdefined, Core.sizeof,
165-
Core.kwfunc, ifelse, Core._typevar, (<:)
165+
Core.kwfunc, Core.ifelse, Core._typevar, (<:)
166166
]
167167

168168
const TOP_TUPLE = GlobalRef(Core, :tuple)

base/compiler/tfuncs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function ifelse_tfunc(@nospecialize(cnd), @nospecialize(x), @nospecialize(y))
231231
end
232232
return tmerge(x, y)
233233
end
234-
add_tfunc(ifelse, 3, 3, ifelse_tfunc, 1)
234+
add_tfunc(Core.ifelse, 3, 3, ifelse_tfunc, 1)
235235

236236
function egal_tfunc(@nospecialize(x), @nospecialize(y))
237237
xx = widenconditional(x)

base/essentials.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,22 @@ Stacktrace:
477477
"""
478478
sizeof(x) = Core.sizeof(x)
479479

480+
"""
481+
ifelse(condition::Bool, x, y)
482+
483+
Return `x` if `condition` is `true`, otherwise return `y`. This differs from `?` or `if` in
484+
that it is an ordinary function, so all the arguments are evaluated first. In some cases,
485+
using `ifelse` instead of an `if` statement can eliminate the branch in generated code and
486+
provide higher performance in tight loops.
487+
488+
# Examples
489+
```jldoctest
490+
julia> ifelse(1 > 2, 1, 2)
491+
2
492+
```
493+
"""
494+
ifelse(condition::Bool, x, y) = Core.ifelse(condition, x, y)
495+
480496
# simple Array{Any} operations needed for bootstrap
481497
@eval setindex!(A::Array{Any}, @nospecialize(x), i::Int) = arrayset($(Expr(:boundscheck)), A, x, i)
482498

base/operators.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -429,22 +429,6 @@ const ≥ = >=
429429
# which is more idiomatic:
430430
isless(x::Real, y::Real) = x<y
431431

432-
"""
433-
ifelse(condition::Bool, x, y)
434-
435-
Return `x` if `condition` is `true`, otherwise return `y`. This differs from `?` or `if` in
436-
that it is an ordinary function, so all the arguments are evaluated first. In some cases,
437-
using `ifelse` instead of an `if` statement can eliminate the branch in generated code and
438-
provide higher performance in tight loops.
439-
440-
# Examples
441-
```jldoctest
442-
julia> ifelse(1 > 2, 1, 2)
443-
2
444-
```
445-
"""
446-
ifelse
447-
448432
"""
449433
cmp(x,y)
450434

0 commit comments

Comments
 (0)