# How do I create syntactic sugar for Julia functions?

**URL:** <https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312>\
**Category:** General Usage\
**Created:** [June 30, 2020, 4:11pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312 "2020-06-30T16:11:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 30, 2020, 4:11pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/1 "2020-06-30T16:11:08Z")

</div>

For example, if I have a function “`f(x, y)`” and want to abbreviate it with “`x S y`”, how can it be done?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 30, 2020, 4:20pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/2 "2020-06-30T16:20:05Z")

</div>

> [@How to define a new infix operator?](https://discourse.julialang.org/t/how-to-define-a-new-infix-operator/34803):
>
> see in base\operators.jl that the built-in operators are defined concisely like: \<(x, y) = isless(x, y) however, seems like I can’t define a custom infix operator like: julia\> (α)(x, y) = x + y α (generic function with 1 method) julia\> α(1, 2) 3 julia\> 1 α 2 ERROR: syntax: extra token "α" after end of expression so, is it possible to have custom infix operators? if yes, what is the syntax of such definitions? thanks.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 30, 2020, 4:51pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/3 "2020-06-30T16:51:53Z")

</div>

If you really want this enough that wrapping your code in a macro is worthwhile you can get it…

```julia
@withSop ...mycode a S b...

```

you’d need to look for the S symbol, and convert it to S(a,b) manually.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 30, 2020, 5:07pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/4 "2020-06-30T17:07:00Z")

</div>

No, that won’t work. A macro can only be applied to syntactically correct code.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 30, 2020, 5:15pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/5 "2020-06-30T17:15:53Z")

</div>

True, you’d have to do `:S`

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 30, 2020, 5:21pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/6 "2020-06-30T17:21:55Z")

</div>

From reading [this thread](https://discourse.julialang.org/t/is-not-an-operator/20221/2), I conclude that some symbols are parsed as infix, but some aren’t, for example

```julia
julia> function ↑(x, y)
           return x - y + 3
       end
↑ (generic function with 1 method)

julia> 9 ↑ 2
10

julia> function f(x, y)
           return x - y + 3
       end
f (generic function with 1 method)

julia> 9 f 2
ERROR: syntax: extra token "f" after end of expression
Stacktrace:
 [1] top-level scope at REPL[10]:0

```

Julia parses `↑` as an infix, but not `f`.

I don’t know if it is possible to make Julia parse `f` as an infix.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 30, 2020, 5:23pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/7 "2020-06-30T17:23:45Z")

</div>

> [@Lucas\_Queiroz](#):
>
> I don’t know if it is possible to make Julia parse `f` as an infix.

It’s not possible… but using the macro approach above, if you did `:f` it would be parsed as “the symbol f” and then you could write your macro to convert

`9 :f 2` into `f(9,2)`

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 30, 2020, 5:28pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/8 "2020-06-30T17:28:00Z")

</div>

I would need an introduction to Julia macros, because I am not very familiar with them.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 30, 2020, 5:32pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/9 "2020-06-30T17:32:14Z")

</div>

> [@dlakelan](#):
>
> and then you could write your macro to convert
> 
> `9 :f 2` into `f(9,2)`

That’s still not valid syntax.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 30, 2020, 6:16pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/10 "2020-06-30T18:16:31Z")

</div>

hmm… my lisp background is showing through. (9 :f 2) is valid lisp, and results in a list of three items… I guess in Julia there’s no equivalent where you just have a list of values? The closes equivalent of that is a tuple (9,:f,2) and that’s no help.

is there any hope for something equivalent to the Lisp:

(my-special-rpn-calc 2 3 + 5 - 33 f )

and have it convert to `f(33,-(5,+(2,3)))`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 30, 2020, 6:21pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/11 "2020-06-30T18:21:13Z")

</div>

You can create a string macro and then do anything at all with the contents of that string…but it’s really not worth the effort. I’d recommend just living within the set of syntax supported by the Julia parser.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 30, 2020, 6:22pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/12 "2020-06-30T18:22:46Z")

</div>

` 9 f 2` is three expressions, which if you have sufficient patience perhaps you could make a macro understand, in some contexts:

```julia
julia> macro pr(exs...)
       @show exs; nothing
       end;

julia> @pr x = 9 f 2
exs = (:(x = 9), :f, 2)

julia> @pr x = (9 f 2)
ERROR: syntax: missing comma or ) in argument list

```

Notice that `x=9` is a completely separate expression, `f` is not part of its right hand side.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 30, 2020, 6:30pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/13 "2020-06-30T18:30:31Z")

</div>

Yes, it doesn’t appear it will be worth the effort of creating a new macro.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 30, 2020, 6:38pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/14 "2020-06-30T18:38:44Z")

</div>

It looks like if you’re committed to getting a sequence of values… you could do:

`@myrpncalc [1 2 + 3 - 55 f]`

and you’d have to fix stuff but it’d parse as an array of things

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 30, 2020, 10:11pm UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/15 "2020-06-30T22:11:28Z")

</div>

I just forked and rewrote the old InfixFunctions.jl trick to work on 1.0: [https://github.com/MasonProtter/InfixFunctions.jl](https://github.com/MasonProtter/InfixFunctions.jl)

```julia
julia> using InfixFunctions

julia> foo(x::Int, y::Int) = 2(x - y)//(x + y)
foo (generic function with 1 method)

julia> @infix foo
[ Info: foo has been infixified
foo (generic function with 1 method)

julia> 1 |foo| 2
-2//3

```

It’s a hack, but it works.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [July 1, 2020, 1:08am UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/16 "2020-07-01T01:08:59Z")

</div>

That’s a very nice trick, it solves my problem.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [July 1, 2020, 1:53am UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/17 "2020-07-01T01:53:43Z")

</div>

I got myself curious about this trick. I am reading the source code but I don’t seem to be understanding because I lack familiarity with macros. But here’s what I understood: “`@infix foo`” creates two new distinct methods for the function “`Base.:|`”, the first creates an `InfixFunction`, and the second applies an operator to the argument. So that when “`1 | foo | 2`” is written in the REPL, first Julia evaluates “`1 | foo`”, yielding an `InfixFunction` as output, and then applies the `InfixFunction` “`1 | foo`” to “`2`”. Did I get it right?

```julia
macro infix(operator::Symbol)
    return quote
        $operator::Function

        function Base.:|(arg₁, infix::typeof($operator))
            return $InfixFunction(arg₂ -> infix(arg₁, arg₂))
        end

        Base.:|(infix::typeof($operator), arg₂) = infix(arg₂)

        @info "$($operator) has been infixified"

        $operator
    end |> esc
end

```

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [July 1, 2020, 2:21am UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/18 "2020-07-01T02:21:32Z")

</div>

Fortunately Julia already supports infixing for a large subset of Unicode characters. All I need to do is suffix those characters with modifying marks and I’m done! Here’s the quote from the [documentation](https://docs.julialang.org/en/v1/manual/variables/)

“Most of the Unicode infix operators (in category Sm), such as ⊕, are parsed as infix operators and are available for user-defined methods (e.g. you can use const ⊗ = kron to define ⊗ as an infix Kronecker product). Operators can also be suffixed with modifying marks, primes, and sub/superscripts, e.g. +̂ₐ″ is parsed as an infix operator with the same precedence as +.”

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [July 1, 2020, 2:53am UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/19 "2020-07-01T02:53:48Z")

</div>

By the way, why is it possible to subscript some characters but not others? For example, why does typing “+\_x-tab” in the REPL work but “+\_z-tab” doesn’t?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 1, 2020, 3:18am UTC](https://discourse.julialang.org/t/how-do-i-create-syntactic-sugar-for-julia-functions/42312/20 "2020-07-01T03:18:57Z")

</div>

> [@Lucas\_Queiroz](#):
>
> Did I get it right?

Yep, you got it.

> [@Lucas\_Queiroz](#):
>
> Fortunately Julia already supports infixing for a large subset of Unicode characters. All I need to do is suffix those characters with modifying marks and I’m done! Here’s the quote from the [documentation](https://docs.julialang.org/en/v1/manual/variables/)
> 
> “Most of the Unicode infix operators (in category Sm), such as ⊕, are parsed as infix operators and are available for user-defined methods (e.g. you can use const ⊗ = kron to define ⊗ as an infix Kronecker product). Operators can also be suffixed with modifying marks, primes, and sub/superscripts, e.g. +̂ₐ″ is parsed as an infix operator with the same precedence as +.”

Yeah, that’s the best way to go about this in general.

> [@Lucas\_Queiroz](#):
>
> By the way, why is it possible to subscript some characters but not others? For example, why does typing “+\_x-tab” in the REPL work but “+\_z-tab” doesn’t?

That’s just because Unicode doesn’t cover the whole alphabet for subscripts and superscipts, despite having every emoji you could imagine. Here’s a wiki page on what’s available: [Unicode subscripts and superscripts - Wikipedia](https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts)

It’s pretty disappointing and not really something we can change on our own, but there has been some thought before about submitting a proposal to the Unicode consortium to fix this: [GitHub - stevengj/subsuper-proposal: Draft proposal for additional sub/superscript characters in Unicode](https://github.com/stevengj/subsuper-proposal) but that proposal is currently laying dormant as far as I’m aware.
