So I'm trying to overload the "+" operator to work with some custom types. Is there any way I can call proc no matter in what order arguments are passed (sort of single-dispatch way) ?
I'd like to avoid the case having to duplicate the whole proc implementation and only change the order of arguments.
What would be an idiomatic Nim's way to handle that? Write a macro?
Thanks,
type AAA = object
val: int
type BBB = object
val: float
proc `+`(a:int, b:float)=
echo "1 - int+float"
# proc `+`(b:float,a:int)=
# echo "2 - float + int"
var aaa : AAA
var bbb : BBB
aaa.val = 1
bbb.val = 1.0
echo "aaa: ", aaa.val," bbb: ", bbb.val
# aaa.val + bbb.val
bbb.val + aaa.val # This won't work
You can certainly write a macro to generate the proc definitions but usually it's enough to let one proc call the other:
proc `+`(a: int, b: float): float =
echo "real implementation here"
proc `+`(a: float, b: int): float = b+a