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

Default leq method for Reals seems inconsistent with docstring #46327

Closed
jishnub opened this issue Aug 12, 2022 · 1 comment · Fixed by #46341
Closed

Default leq method for Reals seems inconsistent with docstring #46327

jishnub opened this issue Aug 12, 2022 · 1 comment · Fixed by #46341
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@jishnub
Copy link
Contributor

jishnub commented Aug 12, 2022

julia> struct A <: Real end

julia> Base.:(==)(::A, ::A) = false

julia> Base.:(<)(::A, ::A) = false

julia> x = A(); y = x;

julia> (x < y) | (x == y)
false

julia> x <= y
ERROR: <= not defined for A
Stacktrace:
 [1] error(::String, ::String, ::Type)
   @ Base ./error.jl:44
 [2] no_op_err(name::String, T::Type)
   @ Base ./promotion.jl:484
 [3] <=(x::A, y::A)
   @ Base ./promotion.jl:501
 [4] top-level scope
   @ REPL[11]:1

help?> <=
search: <=

  <=(x, y)
  (x,y)

  Less-than-or-equals comparison operator. Falls back to (x < y) | (x == y).
[...]

julia> versioninfo()
Julia Version 1.9.0-DEV.1120
Commit 95cbb9f6184 (2022-08-12 00:44 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.5 (ORCJIT, tigerlake)
  Threads: 1 on 8 virtual cores
Environment:
  LD_LIBRARY_PATH = :/usr/lib/x86_64-linux-gnu/gtk-3.0/modules
  JULIA_EDITOR = subl

As per the docstring, <= should fall back to using < and ==.

@mgkuhn mgkuhn added the types and dispatch Types, subtyping and method dispatch label Aug 12, 2022
@mgkuhn
Copy link
Contributor

mgkuhn commented Aug 12, 2022

It seems the line

<=(x, y) = (x < y) | (x == y)

in base/operators.jl is less specific than the line

(<=)(x::T, y::T) where {T<:Real} = no_op_err("<=", T)

in base/promotion.jl, so the latter overrides the former.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants