How to define and call a local function with a strange name?

By “strange” I mean a function name that is not valid without quoting.
I can easily write a remote call to such a function with module eg

Mod.::(1, 2)
Mod.":::"(1, 2)
Mod.";#$%"(1, 2)

However I am unable to guess the syntax for defining and calling such a local function

::(1, 2)
:::(1, 2)
";#$%"(1, 2)
:";#$%"(1, 2)

all give syntax error before: '('

A special case is ::, which turns out to be an operator so I can override and call it with infix syntax

defp a :: b, do: {a, b}

def test, do: 1 :: 2

The :: operator is only used in typespecs and binaries in standard Elixir (I would argue it is more of a syntax element than a standalone operator), I wonder if it has any bad effect on typespecs and binaries if I override it?

Firstly those 2 articles describes everything you should know about operators:

https://www.rodneyfolz.com/custom-infix-functions-in-elixir/

As far as I remember everything else in this topic is not possible in Elixir unless you ask Eliixir Core Team to support that. I don’t believe that they would do so - just want to say that’s possible to support, but not supported as it’s generally not a good idea.


Secondly there were already a similar topic:

It’s a bit of a pain but I figured it out a couple years ago because of integrations with some old Erlang libraries callbacks, you pretty much have to do:

defmodule Blah do
  def unquote(:";#$%")(a, b), do: a + b

  def test() do
    3 == unquote(:";#$%")(1, 2)
  end
end
1 Like

thank you @Eiji and @OvermindDL1
I coulnd’t find the previous topic, which exactly answers my question

1 Like