diff --git a/src/operators.jl b/src/operators.jl index d26ff76..563a970 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -1,7 +1,7 @@ ## Lifted operators importall Base.Operators -import Base: promote_op, abs, abs2, sqrt, cbrt, scalarmin, scalarmax +import Base: promote_op, abs, abs2, sqrt, cbrt, scalarmin, scalarmax, isless using Compat: @functorize # Methods adapted from Base Julia 0.5 @@ -85,7 +85,8 @@ end if VERSION >= v"0.5.0-dev" for op in (:+, :-, :*, :/, :&, :|, :<<, :>>, :(>>>), :(==), :<, :>, :<=, :>=, - :scalarmin, :scalarmax) + :scalarmin, :scalarmax, + :isless) @eval begin # to fix ambiguities null_safe_op{S<:SafeFloats, @@ -130,3 +131,24 @@ for op in (:+, :-, :*, :/, :%, :รท, :&, :|, :^, :<<, :>>, :(>>>), $op{S}(x::Nullable{S}, y::Nullable{Union{}}) = Nullable{S}() end end + +if !method_exists(isless, (Nullable, Nullable)) + function isless{S,T}(x::Nullable{S}, y::Nullable{T}) + # NULL values are sorted last + if null_safe_op(@functorize(isless), S, T) + (!x.isnull & y.isnull) | + (!x.isnull & !y.isnull & isless(x.value, y.value)) + else + if x.isnull + return false + elseif y.isnull + return true + else + return isless(x.value, y.value) + end + end + end + isless(x::Nullable{Union{}}, y::Nullable{Union{}}) = false + isless(x::Nullable{Union{}}, y::Nullable) = false + isless(x::Nullable, y::Nullable{Union{}}) = !x.isnull +end diff --git a/test/operators.jl b/test/operators.jl index 68be476..d6b1f94 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -201,6 +201,17 @@ module TestOperators x = op(Nullable(), Nullable()) @test isa(x, Nullable{Union{}}) && isnull(x) end + + # isless + @test isless(Nullable(u), Nullable(v)) === isless(u, v) + + @test isless(Nullable(u), Nullable(v, true)) === true + @test isless(Nullable(u, true), Nullable(v)) === false + @test isless(Nullable(u, true), Nullable(v, true)) === false + + @test isless(Nullable(u), Nullable()) === true + @test isless(Nullable(), Nullable(v)) === false + @test isless(Nullable(), Nullable()) === false end end end