How to check if a method exists for a specific type?

UPDATE: Thanks to @ rickhg12s for indicating that it appears, I may have come across an error. method_exists(<, (MyType, MyType))returns trueand method_exists(isless, (MyType, MyType))returns false. I'm on v0.3.x, but @ rickhg12s is on the nightly build v0.4, so I will write about the problem.

How to check if a method exists for a specific type?

I thought I could do this, for example:

method_exists(<, (Int, Int))

which will return truebecause less than comparison is defined for two integers. However, when I define my own type:

type MyType; end

and then try:

method_exists(<, (MyType, MyType))

he returns true. But I thought he would return false, since:

a = MyType()
a < a

throws an error because it is <not defined for MyType. So, how can I detect at runtime, is there a method for this type?

+4
1

, method_exists ( ) , , . , - ( ) . , .

@rickhg12s , method_exists(<, (MyType, MyType)) true, method_exists(isless, (MyType, MyType)) false.

? , julia , , , , , .

< isless - . , < , < . isless. , < , , . method_exists(<, (MyType, MyType)) true. isless, , " " - , , , isless , Julia , , method_exists(isless, (MyType, MyType)) false.

, , . , method_exists , , " " .

+6

Source: https://habr.com/ru/post/1589102/


All Articles