# Cursed Custom Infix Operators

**URL:** <https://discourse.julialang.org/t/cursed-custom-infix-operators/131915>\
**Category:** General Usage\
**Tags:** fun\
**Created:** [August 28, 2025, 4:25pm UTC](https://discourse.julialang.org/t/cursed-custom-infix-operators/131915 "2025-08-28T16:25:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [August 28, 2025, 4:25pm UTC](https://discourse.julialang.org/t/cursed-custom-infix-operators/131915/1 "2025-08-28T16:25:02Z")

</div>

Reading this [StackExchange page on custom infix operators](https://langdev.stackexchange.com/questions/2761/declaring-infix-operators-like-haskells-in-other-languages) got me thinking about Julia’s special casing for infix operators and if there was any way to get that kind of expressiveness. Regular macros won’t work since you need to source expression to be parseable.

Then, a totally cursed way to do this occurred to me.

```julia
julia> abstract type Infixable <: Function end

julia> struct Infixed{T,O}
           x::T
           op::O
       end

julia> Base.rem(x, op::Infixable) = Infixed(x, op)

julia> Base.rem(infixed::Infixed{<:Any, O}, y) where O = (infixed.op::O)(infixed.x, y)

julia> struct f <: Infixable end

julia>(::typeof(f))(a,b)=a+b

julia> 2 %f% 3
5

julia> 2 %f% 3 + 5
10

```

I checked to make sure that `%` doesn’t have any methods that aren’t typed with some kind of `Number` or `Date`, so I’m reasonably certain this doesn’t cause any invalidations, but I didn’t check any of the other operators.

That said, you can pick your precedence by changing the operator you use as the wrapper.

PS I’m not saying this advisable… But I am saying Julia’s type system is dope.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 28, 2025, 4:32pm UTC](https://discourse.julialang.org/t/cursed-custom-infix-operators/131915/2 "2025-08-28T16:32:15Z")

</div>

> [@mrufsvold](#):
>
> Then, a totally cursed way to do this occurred to me.

In fact, this was already discussed in [julia#24404](https://github.com/JuliaLang/julia/pull/24404#issuecomment-341570934) and [julia#16985](https://github.com/JuliaLang/julia/issues/16985), inspired by a [similar hack in Python](https://pypi.org/project/infix/), and was implemented in [GitHub - Ismael-VC/InfixFunctions.jl: Julia infix function hack.](https://github.com/Ismael-VC/InfixFunctions.jl)

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [August 28, 2025, 4:44pm UTC](https://discourse.julialang.org/t/cursed-custom-infix-operators/131915/3 "2025-08-28T16:44:32Z")

</div>

Awesome! I searched the forum and couldn’t find mention of this. Sorry for the redundant post.
