Aside +, *, etc. is there a way to define my own binary operator, say Foo such that I can write something like:
Foo(x,y) = [some funny op using x and y]
((x Foo b) Foo c) ....
Aside +, *, etc. is there a way to define my own binary operator, say Foo such that I can write something like:
Foo(x,y) = [some funny op using x and y]
((x Foo b) Foo c) ....
You’re looking for custom infix operators - see also ★ is not an operator.
In short - no.
Saw it.
Not even with a macro, e.g.
@opdef Foo(x,y) = [some funny op using x and y]
@op ((x Foo b) Foo c) ....
?
I think with macros you could build something similar. Just remember that a macro needs to take valid Julia syntax as input and (a foo b) is not valid syntax but e.g. [a foo b] would be. So in principle you could cobbled together something. However, in practice I would advise against using macros to bend a part of Julia’s core syntax to your will like this. It will quickly make your code incomprehensible to anybody else and probably contain all sorts of edge cases.
However, you have infinitely many choices available if you want to define a new infix operator: On adjoints and custom postfix and infix operators - #35 by stevengj
(And there is even a hack to make completely arbitrary identifiers like Foo act infix-like, though I wouldn’t recommend using it in production code: see Cursed Custom Infix Operators and InfixFunctions.jl.)
Oh my, I was not aware of these at all! Cursed indeed..