diff --git a/NEWS.md b/NEWS.md index b50c29cd2b337..33fedc02e3994 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ New language features --------------------- * `CartesianIndices` can now be constructed from two `CartesianIndex`es `I` and `J` with `I:J` ([#29440]). + * `isnothing(::Any)` function can now be called to check whether something is a `Nothing`, returns a `Bool` ([#29679]) Language changes ---------------- diff --git a/base/exports.jl b/base/exports.jl index e7cdc8808100b..5da9a06207a62 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -669,6 +669,7 @@ export missing, skipmissing, something, + isnothing, # time sleep, diff --git a/base/some.jl b/base/some.jl index fe4ced04e0bb4..57d105d80959b 100644 --- a/base/some.jl +++ b/base/some.jl @@ -40,6 +40,15 @@ Throw an error if `x === nothing`, and return `x` if not. notnothing(x::Any) = x notnothing(::Nothing) = throw(ArgumentError("nothing passed to notnothing")) +""" + isnothing(x) + +Return `true` if `x === nothing`, and return `false` if not. +""" +isnothing(::Any) = false +isnothing(::Nothing) = true + + """ something(x, y...) diff --git a/doc/src/base/base.md b/doc/src/base/base.md index 0567e934a5944..e8481b7b5e787 100644 --- a/doc/src/base/base.md +++ b/doc/src/base/base.md @@ -199,6 +199,7 @@ Core.NamedTuple Base.Val Core.Vararg Core.Nothing +Base.isnothing Base.Some Base.something Base.Enums.@enum diff --git a/test/some.jl b/test/some.jl index ffa76623b1839..e368c7b27c5dc 100644 --- a/test/some.jl +++ b/test/some.jl @@ -94,3 +94,7 @@ b = [ "replacement", "replacement", nothing, missing ] using Base: notnothing @test notnothing(1) === 1 @test_throws ArgumentError notnothing(nothing) + +# isnothing() +@test !isnothing(1) +@test isnothing(nothing)