What is happening here? '<~>' and '<|>'

Alright, so I was was checking out the documentation for Quark, and I saw that there were 2 compose functions –

composed = f <~> g


composed = g <|> f

I understand what is happening, but not how such a function is being defined / called with Elixir syntax. From the Quark source,

  @spec fun <|> fun :: fun
  def g <|> f, do: compose(g, f)


  @spec fun <~> fun :: fun
  def f <~> g, do: compose_forward(f, g)

So I hopped in and tried to test it:

defmodule Testing do
  def a <|> b do
    a + b

And then in iex:

iex(1)> 1 <|> 2
** (CompileError) iex:1: undefined function <|>/2

which is weird, because it means that even though I’m not defining a function <|>, I’m calling it.

Then I realized, oh, this function is defined inside the module so I should try

iex(1)> 1 Testing.<|> 2
** (SyntaxError) iex:1: syntax error before: 'Testing'

But this worked:

defmodule Testing do
  def a <|> b do
    a + b

  def go do
    1 <|> 2
iex(1)> Testing.go

I tried replacing <|> with some things other than <~>, which I also used in Quartz, but none of them seemed to work.

What’s happening here! Where is it documented!

The doc is here: https://hexdocs.pm/elixir/operators.html#custom-and-overridden-operators

If you don’t import the module, I think you need to use Testing.<|>(1, 2) rather than 1 Testing.<|> 2.

1 Like