4 ms·
Elixir allows overloading the operators or creating custom ones, but only certain symbols are recognized in the parser, to add new operators the Elixir parser n
by africanboy 6y ago
Elixir allows overloading the operators or creating custom ones, but only certain symbols are recognized in the parser, to add new operators the Elixir parser needs to be changed.
You can see them here https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_parser.yrl#L51-L79 https://github.com/elixir-lang/elixir/blob/master/lib/elixir...
Not all of them are defined in the language, the list of the unused infix operator includes
\\, <-, |, ~>>, <<~, ~>, <~, <~>, <|>, <<<, >>>, |||, &&&, and ^^^
to define a custom infix operator you can use this syntax
def left <|> right do
IO.puts("#{left} <|> #{right}")
end
# usage
left MyModule.<|> right
# or
import MyModule
left <|> right
if the operator is already defined in Kernel, you can exclude it from import and use your own
import Kernel, except: [{:+, 2}]
import MyModule
left + right
as you can see @ is not among them, @ it's not a binary operator and is defined as Kernel.@/1