# Technical term for "operator inference" related to multiple dispatch

**URL:** <https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109>\
**Category:** General Usage\
**Tags:** multidispatch, operator, multiple-dispatch\
**Created:** [May 10, 2024, 2:25pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109 "2024-05-10T14:25:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 10, 2024, 2:25pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109/1 "2024-05-10T14:25:01Z")

</div>

I have defined the operator `<` for my custom type `Person`:

```julia
begin
	struct Person
	    name::String
	    age::Int
	end
	
	import Base: <
	
	function <(a::Person, b::Person)::Bool
	    return a.age < b.age
	end
	
	# Example usage
	person1 = Person("Alice", 30)
	person2 = Person("Bob", 25)
	
	println(person1 < person2) # This will print "false" because 30 is not less than 25
	println(person2 < person1) # This will print "true" because 25 is less than 30

    # ---- inference ??????
	println(person2 > person1) # This will print "false" because 25 is less than 30
	
end

```

In my last `println`, it seems that Julia _inferred_ how the `>` operator should behave for my custom type, even though I didn’t define it, which is great. 👏

My question is, what is this _mechanism_ called? (By ‘mechanism’, I mean the behavior where, if the user defines operator X, Julia generates code for operator Y, given that there is a relationship between X and Y.)

N.B.: Sorry for the horrible title; I’m not sure how to express this.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 10, 2024, 2:33pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109/2 "2024-05-10T14:33:46Z")

</div>

This occurs because how `>` is implemented:

```julia
>(x, y) = y < x

```

I don’t know if there is any sophysticated thing to call it 🙂 . If you implemented `>` first, you would have find it less magical:

```julia
julia> struct A x end

julia> import Base: >

julia> >(a::A, b::A) = a.x > b.x
> (generic function with 4 methods)

julia> A(2) > A(1)
true

julia> A(2) < A(1)
ERROR: MethodError: no method matching isless(::A, ::A)

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 10, 2024, 2:37pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109/3 "2024-05-10T14:37:44Z")

</div>

We often call these sorts of definitions “generic” or “fallback” implementations. Functions in Julia have a meaning — and in some cases it’s possible to define implementations for `Any` arguments based on other methods.

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 10, 2024, 3:06pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109/4 "2024-05-10T15:06:09Z")

</div>

Ah, you’re right, it makes sense… I also have just found in the docs:

> Generally, new types should implement `<` instead of this function, and rely on the fallback definition `>(x, y) = y < x`.

EDIT: it’s less impressive now, because I thought there is some sort of multiple dispatch mechanism that infers this, but it’s actually just the implementation

thank you

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 10, 2024, 3:51pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109/5 "2024-05-10T15:51:27Z")

</div>

> [@younes](#):
>
> EDIT: it’s less impressive now, because I thought there is some sort of multiple dispatch mechanism that infers this, but it’s actually just the implementation

Matter of perspective 🙂 One could also find it impressive how easy it is to write generic, performant code. The default implementation in case implements `>` for all arguments _exactly_ as fast as the hand-written counterpart `<` in a single line of code. Sure while this is an almost trivial example, it does still show the simplicity and power of Julia imo 🙂

---

<div class="post-metadata">

**Author:** ![younes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/younes/32/208506_2.png) [@younes](https://discourse.julialang.org/u/younes)\
**Post date:** [May 10, 2024, 5:19pm UTC](https://discourse.julialang.org/t/technical-term-for-operator-inference-related-to-multiple-dispatch/114109/6 "2024-05-10T17:19:45Z")

</div>

I agree, and I don’t deny that 🙂  
I was referring to that “imaginary/magical mechanism” that _does_ the inference 🤣
