# How to test whether comparison is defined for a type?

**URL:** <https://discourse.julialang.org/t/how-to-test-whether-comparison-is-defined-for-a-type/16583>\
**Category:** General Usage\
**Created:** [October 20, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-test-whether-comparison-is-defined-for-a-type/16583 "2018-10-20T23:36:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)\
**Post date:** [October 20, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-test-whether-comparison-is-defined-for-a-type/16583/1 "2018-10-20T23:36:18Z")

</div>

I defined a new type (`Mod29` – integers mod 29).

```julia
struct Mod29 <: Real
    value::Int8
    function Mod29(value::Integer)
        new(Int8(mod(value, 29)))
    end
end

```

In another, generic, algorithm, I need to know whether comparison is defined on a type. I did not define comparison on `Mod29`, and indeed `Mod29(1) < Mod29(3)` fails:

```julia
**julia&gt;** Mod29(1) &lt; Mod29(3)
**ERROR:** &lt; not defined for Mod29
Stacktrace:
 [1] **error(** ::String, ::String, ::Type **)** at **./error.jl:42**
 [2] **no_op_err(** ::String, ::Type **)** at **./promotion.jl:410**
 [3] **&lt;(** ::Mod29, ::Mod29 **)** at **./promotion.jl:426**
 [4] top-level scope at **none:0**

```

But `hasmethod(<, (Mod29, Mod29))` and `hasmethod(isless, (Mod29, Mod29))` both return `true`.

I see that `<` and `isless` fail trying to do promotion, but I don’t see how to tell that ‘\<’ will fail, without trying it and catching the error.

For your amusement, the algorithm I need to change is LU decomposition, where it tries to pivot on the max. If comparison isn’t defined, I just want it to find an element with an inverse, to pivot on. I want to be able to take inverses of matrices of modular numbers.

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [October 22, 2018, 1:40am UTC](https://discourse.julialang.org/t/how-to-test-whether-comparison-is-defined-for-a-type/16583/2 "2018-10-22T01:40:45Z")

</div>

I think the reason your code is failing is because `hasmethod(<,(Real,Real))` is true, and you have defined `Mod29` as a subtype of `Real`. Is that subtyping necessary? When you declare a subtype like this, you are making an implicit promise to support all the methods that go with `Real`.

One “julian” technique to deal with this problem is to define some new methods:

```julia
   haslessthan(::Any) = false
   haslessthan(::Real) = true
   haslessthan(::Mod29) = false

```

and then dispatch your gaussian elimination on `Val{haslessthan(eltype(A))}` where `A` is the matrix. If done correctly, the dispatch is all computed at compile time so there is no run-time overhead in determining which is the correct gaussian elimination method.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 22, 2018, 2:33am UTC](https://discourse.julialang.org/t/how-to-test-whether-comparison-is-defined-for-a-type/16583/3 "2018-10-22T02:33:31Z")

</div>

also see [Examples/ModInts.jl at master · JuliaAttic/Examples · GitHub](https://github.com/JuliaAttic/Examples/blob/master/ModInts.jl)
