LoginSignup
4
0

More than 3 years have passed since last update.

Elixirのモジュール属性定義でパターンマッチがしたかったけどできなくて泣いちゃった

Posted at

発端

プログラミングElixir 7章の練習問題でシーザー暗号を作る際に、モジュールで'a''z'の数値を定義しておきたかったので、パターンマッチしてくれねぇかなとおもったけど出来ませんでした

環境は以下

$ elixir -v
Erlang/OTP 23 [erts-11.1.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Elixir 1.11.2 (compiled with Erlang/OTP 23)

理想

defmodule MyModule do
  [@a_val, @z_val] 'az'
end

ざんねんながらNG泣いちゃった。

** (SyntaxError) practice7/caesar.exs:4:20: syntax error before: "az"
    (elixir 1.11.2) lib/kernel/parallel_compiler.ex:315: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

最終的にこの形になりました

defmodule MyModule do
  @a_val Enum.at('a', 0)
  @z_val Enum.at('z', 0)
end

動けばOKではありますけど、気持ち悪いので何か良い書き方とかあったらコメントいただけると嬉しいです

4
0
2

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