レコードを使用して引用するquote do:
と、レコード フィールドを含むタプルに変換されません。
iex(1)> quote do: is_bitstring("blah")
{:is_bitstring, [context: Elixir, import: Kernel], ["blah"]}
iex(2)> quote do: Computer.new("Test")
{{:., [], [{:__aliases__, [alias: false], [:Computer]}, :new]}, [], [[name: "Test"]]}
iex(3)> quote do: Computer.new("Test")
{{:., [], [{:__aliases__, [alias: false], [:Computer]}, :new]}, [], [[name: "Test"]]}
iex(4)> c = Computer.new("Test")
Computer[name: "Test", type: nil, processor: nil, hard_drives: []]
iex(5)> c
Computer[name: "Test", type: nil, processor: nil, hard_drives: []]
iex(6)> quote do: c
{:c, [], Elixir}
また、コードでこれを実行しようとすると:
defmacro computer([do: code]) do
# macro login here
# build computer record based on macro logic
computer = Computer.new(params)
quote do: unquote computer
end
エラーが発生します:
** (CompileError) elixir/test/lib/computer_dsl_test.exs: 引用された式のタプルには 2 つまたは 3 つの項目が必要です。無効な引用された式: Computer[name: "", type: nil, processor: nil, hard_drives: []]
レコードは、ある種のラッパー関数を備えた単なるタプルだと思いました。Elixir Getting Started ガイド"A record is simply a tuple where the first element is the record module name."
には、足りないものがありますか? と記載されています。タプル表現を取得するためにレコードで呼び出すことができる関数はありますか? オプションは知っていraw: true
ますが、既存のレコードでそれを使用する方法がわかりません。
洞察はありますか?