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

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), we can dig into it.

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

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).

4 Likes