2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカルでErlangを実行する時の注意点

Last updated at Posted at 2025-03-21

はじめに

Livebookでは問題なく機能するErlangのモジュールが動かないケースがあります。
elixir_を付けたモジュール名にすることで解消します。
当記事では実際のコードを参考にelixir_モジュールを付けた書き方について解説します。

環境

windows11
wsl2
Ubuntu24.04
Erlang/OTP 26 [erts-14.2.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]
Elixir 1.18.2 (compiled with Erlang/OTP 25)

実行結果

モジュール名を変化させないで通る場合

iex > :timer.tc(Process, :sleep, [1000])
{1000246, :ok}

特殊なケース

iex > :json.decode("1")
** (UndefinedFunctionError) function :json.decode/1 is undefined (module :json is not available). Did you mean:

      * :elixir_json.decode/1

    :json.decode("1")
    iex:2: (file)

モジュール名にelixir_を付けることで解消

iex > :elixir_json.decode("1")
1

なおelixir_をつければいいというわけではないのが以下の検証結果からわかる

iex > :elixir_timer.tc(Process, :sleep, [1000])
** (UndefinedFunctionError) function :elixir_timer.tc/3 is undefined (module :elixir_timer is not available)
    :elixir_timer.tc(Process, :sleep, [1000])
    iex:2: (file)

終わりに

elixir_を付けないと機能しない理由についてはドキュメントにも書かれていませんでした。
またこのやり方について記載された記事をすぐに見つけることもできず解消に時間がかかってしまいました。
冷静にエラーメッセージに指摘されている書き方を真似すれば問題はありませんが視野が狭くなると思わぬミスを誘発する可能性があります。
他のモジュールについてもelixir_が必要なパターンと必要でないパターンがあるかもしれないため上手く動かない時はエラーメッセージに記載されているモジュール名を採用することを強くオススメします。
なおLivebook上ではこの問題は発生しなかったため、iexで実行する時限定の問題と考えられます。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?