# If f = +, why can't I use f as an infix operator (e.g., 2 f 3 == 5)?

**URL:** <https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698>\
**Category:** New to Julia\
**Tags:** infix\
**Created:** [August 20, 2021, 1:46am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698 "2021-08-20T01:46:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 1:46am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/1 "2021-08-20T01:46:28Z")

</div>

3 + 4 gives the usual result. f = + followed by 3 f 4 gives an error.

Why doesn’t this work?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 20, 2021, 1:48am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/2 "2021-08-20T01:48:30Z")

</div>

Only some operators have infix syntax.

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 1:54am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/3 "2021-08-20T01:54:51Z")

</div>

Yes, and + is one of them. Why does + work and f does not, when f == x?

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [August 20, 2021, 1:56am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/4 "2021-08-20T01:56:10Z")

</div>

You can do

```julia
julia> f = +
+ (generic function with 190 methods)

julia> f(3, 4)
7

julia> +(3,4) # above is same as 
7

```

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 1:56am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/5 "2021-08-20T01:56:27Z")

</div>

The same problem occurs with +(3,4) vs. f(3,4) when f == +

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 1:57am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/6 "2021-08-20T01:57:32Z")

</div>

That works on the command line, but when I try it in code, it fails.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [August 20, 2021, 2:03am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/7 "2021-08-20T02:03:34Z")

</div>

This comment suggests your mental model is that the bound values of operators determine what syntax is allowed, but the syntax is not influenced by values at all.

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 2:08am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/8 "2021-08-20T02:08:40Z")

</div>

Here is the function that fails:

function apply(f)  
printstyled(“\nenter apply”, color=:green)  
ar = methods(+).ms[1].nargs-1 # get function arity   
if ar == 1  
x = pop()  
push(f(x))  
return  
elseif ar == 2

```
	printstyled("\nS = $S", color = :green)

	x = pop()
	y = pop()

	printstyled("\napply:f=$f x=$x y=$y ", color=:green)

	push(f(x,y))
	return
elseif ar == 3
	x = pop() 
	x = pop()
	z = pop()
	push(f(x,y,z)) 
end
printstyled("\nleave apply", color = :green)

```

end

The error occurs at the ar == 2 part.

Clearly the runtime variables are as follows: f = +, x = 3, and y = 2. So, f(x,y) should result in 5.

But here is the resulting console output:

enter apply  
S = Stack{Any}(Deque [Any[2, 3]])  
apply:f=+ x=3 y=2  
**ERROR:** MethodError: objects of type SubString{String} are not callable

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 2:13am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/9 "2021-08-20T02:13:34Z")

</div>

Why should code that works on the command line not work in code? That conflicts with the entire idea of developing interactively, then putting the checked out code in a file.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [August 20, 2021, 2:17am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/10 "2021-08-20T02:17:02Z")

</div>

It works for me in code. File foo.jl

```julia
function foo(f, x, y)
    f(x,y)
end

```

```julia
julia> include("foo.jl")
foo (generic function with 1 method)

julia> foo(+,3,4)
7

```

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 20, 2021, 2:18am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/11 "2021-08-20T02:18:26Z")

</div>

Unfortunately none of us can run that example because none of `push`, `pop` and `S` are defined. If you could provide those and reduce it into a MWE (see [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/)), we can dig into it.

> [@dgpdx](#):
>
> Why should code that works on the command line not work in code? That conflicts with the entire idea of developing interactively, then putting the checked out code in a file.

I just tried putting this into a file and running it:

```julia
f = +
x, y = 2, 3
println(f(x, y))
println(+(x, y))

```

As expected, it prints out 5 twice.

Edit: to elaborate on @johnmyleswhite’s point, most languages with infix operators (i.e. not lisps or forths) allow a limited number of symbols in infix position. This is a parsing restriction as opposed to a runtime one, so while you can overload the _meaning_ of those symbols, you can not use symbols or identifiers that have not been explicitly allowed by the parser in infix position. There are languages that allow arbitrary infix functions (e.g. Kotlin, Haskell), but they must be escaped with something like `lhs `infixop` rhs` (as opposed to native infix functions, which do not require escaping).

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 2:21am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/12 "2021-08-20T02:21:46Z")

</div>

OK, I guess I must use that format. Thanks for the input!

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 2:25am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/13 "2021-08-20T02:25:04Z")

</div>

The only difference I can is that I’m passing the function in to apply and popping x and y off a stack.

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 2:26am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/14 "2021-08-20T02:26:22Z")

</div>

I take your point about infix functions. Thanks!

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 20, 2021, 2:27am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/15 "2021-08-20T02:27:25Z")

</div>

The big difference is that `f` has mysteriously become a string instead of a reference to a function (hence `MethodError: objects of type **SubString{String}** are not callable`). That shouldn’t be happening from the snippet you posted, so some external code must be to blame.

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 2:29am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/16 "2021-08-20T02:29:19Z")

</div>

I thought that also, but no other code is running. Notice the printout that clearly shows  
f = + and is not a string. Very strange…

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 20, 2021, 4:09am UTC](https://discourse.julialang.org/t/if-f-why-cant-i-use-f-as-an-infix-operator-e-g-2-f-3-5/66698/18 "2021-08-20T04:09:08Z")

</div>

I found the problem, cockpit error as usual. Thanks for all the help!
