# How to Use Operator Overloading with Optional Arguments in Julia

**URL:** <https://discourse.julialang.org/t/how-to-use-operator-overloading-with-optional-arguments-in-julia/114375>\
**Category:** General Usage\
**Tags:** function, operator\
**Created:** [May 16, 2024, 7:59pm UTC](https://discourse.julialang.org/t/how-to-use-operator-overloading-with-optional-arguments-in-julia/114375 "2024-05-16T19:59:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akhil\_Akkapelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhil_akkapelli/32/202792_2.png) [@Akhil\_Akkapelli](https://discourse.julialang.org/u/Akhil_Akkapelli)\
**Post date:** [May 16, 2024, 7:59pm UTC](https://discourse.julialang.org/t/how-to-use-operator-overloading-with-optional-arguments-in-julia/114375/1 "2024-05-16T19:59:35Z")

</div>

I’m working on a Julia project and I’ve defined a custom function with operator overloading. My function, which I’ve defined using the `|` operator, initializes a matrix of `Box` objects with properties derived from a parent `Box`. Here is my function definition:

```julia
function |(ChildBox::Matrix{Box}, ParentBox::Box; Ratio::Matrix{T} where {T<:Real}=ones(size(ChildBox)))
  println(Ratio)
  .
  #function definition
  .
end

```

I want to call this function using the operator style. The call `B2 | B1[1,1]` works fine. However, I also want to pass the optional `Ratio` argument in the operator style, something like `B2 | B1[1,1]; Ratio=[1 2 1; 2 4 2]`.

How can I achieve this in Julia? Is there a way to properly call the overloaded operator with an optional argument in this manner?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 16, 2024, 8:01pm UTC](https://discourse.julialang.org/t/how-to-use-operator-overloading-with-optional-arguments-in-julia/114375/2 "2024-05-16T20:01:01Z")

</div>

No you can’t - you need the prefix style to pass kw arguments.

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [May 16, 2024, 8:56pm UTC](https://discourse.julialang.org/t/how-to-use-operator-overloading-with-optional-arguments-in-julia/114375/3 "2024-05-16T20:56:44Z")

</div>

Although this may not be the solution you are looking for, there is a workaround with macros.  
For example, `@test` provides the support of infix ≈ with kwargs.  
[https://docs.julialang.org/en/v1/stdlib/Test/#Other-Test-Macros](https://docs.julialang.org/en/v1/stdlib/Test/#Other-Test-Macros)
