Croma.StructCallSyntax

This module provides a new syntax with ~> operator for calls to functions that take structs as 1st argument.

To enable the syntax import this module:

import Croma.StructCallSyntax

This module is not imported by use Croma. It is necessary to explicitly import this module.

Examples

iex> import Croma.StructCallSyntax
...> defmodule S do
...>   defstruct [:a, :b]
...>   def f(s, i) do
...>     s.a + s.b + i
...>   end
...> end

...> s = %S{a: 1, b: 2}
...> s~>f(3)              # => S.f(s, 3)
6

Note that calling functions with the ~> syntax involves the following tradeoff:

It is recommended that you think carefully about the above pros/cons for using this module.

Source

Summary

struct ~> arg2

A macro that provides a syntax that resembles function invocations in typical OOP languages

Macros

struct ~> arg2

A macro that provides a syntax that resembles function invocations in typical OOP languages.

The module that defines both the “receiver” struct (i.e. left hand side of ~>) is extracted at run-time. The extracted module must define the target function. The “receiver” is then passed as the 1st argument to the function.

Source