# Overloading ()

**URL:** <https://discourse.julialang.org/t/overloading/63649>\
**Category:** New to Julia\
**Created:** [June 27, 2021, 3:26pm UTC](https://discourse.julialang.org/t/overloading/63649 "2021-06-27T15:26:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [June 27, 2021, 3:26pm UTC](https://discourse.julialang.org/t/overloading/63649/1 "2021-06-27T15:26:41Z")

</div>

Dear all,

I am trying to overload the () operator in order to simulate executability of an object type.  
Basically, this is what I got:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 27, 2021, 3:27pm UTC](https://discourse.julialang.org/t/overloading/63649/2 "2021-06-27T15:27:28Z")

</div>

`()` 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)`).

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [June 27, 2021, 3:31pm UTC](https://discourse.julialang.org/t/overloading/63649/3 "2021-06-27T15:31:49Z")

</div>

I see, so the unfortunate takeaway is that I cannot do this?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 27, 2021, 3:34pm UTC](https://discourse.julialang.org/t/overloading/63649/4 "2021-06-27T15:34:26Z")

</div>

I think you want to do the following

```julia
julia> struct A
           x
       end;

julia> (a::A)() = a.x

julia> t = A(1);

julia> t()
1

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [June 27, 2021, 3:35pm UTC](https://discourse.julialang.org/t/overloading/63649/5 "2021-06-27T15:35:14Z")

</div>

Is this what you want? It sounds like it.

```julia
function (L::Lagrangian{F})(q::Vector{G}, v::Vector{G}) where {F<:Real, G<:Real}
	eval(L, q, v)
end

```

See [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 27, 2021, 3:37pm UTC](https://discourse.julialang.org/t/overloading/63649/6 "2021-06-27T15:37:57Z")

</div>

OP should definitely not be using `eval`. `eval` is pretty much never the solution someone is looking for and is indicative of an XY problem.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 27, 2021, 3:38pm UTC](https://discourse.julialang.org/t/overloading/63649/7 "2021-06-27T15:38:19Z")

</div>

The `eval` is redundant - in that definition, you can just write your code however you’d like and still have access to `L`, `q` and `v`.

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [June 27, 2021, 3:47pm UTC](https://discourse.julialang.org/t/overloading/63649/8 "2021-06-27T15:47:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [June 27, 2021, 3:47pm UTC](https://discourse.julialang.org/t/overloading/63649/9 "2021-06-27T15:47:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [June 27, 2021, 3:48pm UTC](https://discourse.julialang.org/t/overloading/63649/10 "2021-06-27T15:48:39Z")

</div>

True enough, but should Multiple Dispatch not take care of it? Or did I understand that wrong?

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [June 27, 2021, 4:16pm UTC](https://discourse.julialang.org/t/overloading/63649/11 "2021-06-27T16:16:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [June 27, 2021, 4:18pm UTC](https://discourse.julialang.org/t/overloading/63649/12 "2021-06-27T16:18:41Z")

</div>

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!
