LoginSignup
7
6

More than 5 years have passed since last update.

Elixir で普通では使えない記号が入った名前の関数を定義する

Posted at

quote を使うとめちゃくちゃな名前の関数でも定義できる。

fancy.exs
defmodule Fancy do
  defmacro __using__(_) do
    fancy_name = :"!#$%&'()_"
    quote do
      def unquote(fancy_name)() do
        42
      end
    end
  end
end
foo.exs
defmodule Foo do
  use Fancy
end
$ iex -r fancy.exs -r foo.exs
iex> Foo."!#$%&'()_"
42
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6