I am trying to overload the () operator in order to simulate executability of an object type.
Basically, this is what I got:
import Base: ()
Base.:()(L::Lagrangian{F}, q::Vector{G}, v::Vector{G}) where {F<:Real, G<:Real}
eval(L, q, v)
end
and the first line fails with syntax: invalid "import" statement: expected identifier, which suggest to me that the ()operator/function is not imported that way. As such, my question would be: Is there a way to overload the parentheses? And if yes, how?
Please note that there are probably two different parentheses, namely the ones for expression grouping (a + b) + c and the “function call” parentheses f(). I am only interested in the latter.
PS. I realize this may be an extremely naive question so sorry in advance if I should have looked elsewhere before posting.
() is not an operator and not an identifier. It’s just syntax which, depending on what else is written, may denote a function call (such as f()) or construct a tuple (such as (1,2,3)).
Oh, I didn’t even catch that OP used eval. They should definitely not use that. I think they just called their function eval, unaware of the fact that every module already defines an eval function that does something that is probably unrelated to what they want to do.
That is indeed what I wanted, thank you for the link as well.
@pdeffebach Any chance you could expand? I am familiar with the XY problem but I fail to see how this is it… Although I don’t doubt that having both the functor definition and the eval function is redundant
Your three-argument eval method and the original one-argument one can coexist, it should not cause problems. However, it may be confusing to add methods to eval instead of using a new function name, since your usage is unrelated to the original purpose of the eval function.
I have just remembered what eval was originally for and I realize that you guys make a very good point, so I shall remove the eval() function in favour of the functor behavior. Thank you very much for explaning!