Technical term for "operator inference" related to multiple dispatch

younes
Fri, 10 May 2024 14:25:01 +0000
General Usage

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

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.

6 posts - 4 participants