How to Use Operator Overloading with Optional Arguments in Julia

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:

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?

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

1 Like

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

1 Like