【发布时间】:2016-03-21 21:20:34
【问题描述】:
作为我们 Dev Book Club 的一员,我在 Elixir 中编写了一个随机密码生成器。决定玩元编程,并用宏编写它来干点事情。
这很好用:
# lib/macros.ex
defmodule Macros do
defmacro define_alphabet(name, chars) do
len = String.length(chars) - 1
quote do
def unquote(:"choose_#{name}")(chosen, 0) do
chosen
end
def unquote(:"choose_#{name}")(chosen, n) do
alphabet = unquote(chars)
unquote(:"choose_#{name}")([(alphabet |> String.at :random.uniform(unquote(len))) | chosen], n - 1)
end
end
end
end
# lib/generate_password.ex
defmodule GeneratePassword do
require Macros
Macros.define_alphabet :alpha, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Macros.define_alphabet :special, "~`!@#$%^&*?"
Macros.define_alphabet :digits, "0123456789"
def generate_password(min_length, n_special, n_digits) do
[]
|> choose_alpha(min_length - n_special - n_digits)
|> choose_special(n_special)
|> choose_digits(n_digits)
|> Enum.shuffle
|> Enum.join
end
end
我想在字典/地图甚至列表中定义字母,并对其进行迭代以调用 Macros.define_alphabet,而不是手动调用 3 次。但是,当我尝试使用下面的代码时,无论我使用什么结构来保存字母,它都会导致编译失败。
alphabets = %{
alpha: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
special: "~`!@#$%^&*?",
digits: "0123456789",
}
for {name, chars} <- alphabets, do: Macros.define_alphabet(name, chars)
给出以下错误:
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Compiled lib/macros.ex
== Compilation error on file lib/generate_password.ex ==
** (FunctionClauseError) no function clause matching in String.Graphemes.next_grapheme_size/1
(elixir) unicode/unicode.ex:231: String.Graphemes.next_grapheme_size({:chars, [line: 24], nil})
(elixir) unicode/unicode.ex:382: String.Graphemes.length/1
expanding macro: Macros.define_alphabet/2
lib/generate_password.ex:24: GeneratePassword (module)
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
我尝试过将字母表映射为列表列表、元组列表、原子映射->字符串和字符串->字符串,这似乎并不重要。我还尝试将这些对通过管道传递给 Enum.each 而不是使用“for”理解,如下所示:
alphabets |> Enum.each fn {name, chars} -> Macros.define_alphabet(name, chars) end
它们都给出相同的结果。认为这可能与调用 :random.uniform 有关,并将其更改为:
alphabet |> to_char_list |> Enum.shuffle |> Enum.take(1) |> to_string
这只是将错误稍微改变为:
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
== Compilation error on file lib/generate_password.ex ==
** (Protocol.UndefinedError) protocol String.Chars not implemented for {:name, [line: 24], nil}
(elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) lib/string/chars.ex:17: String.Chars.to_string/1
expanding macro: Macros.define_alphabet/2
lib/generate_password.ex:24: GeneratePassword (module)
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
即使进行了这种更改,当我像顶部一样手动调用 Macros.define_alphabet 时,它也可以正常工作,但当我以任何理解方式或使用 Enum.each 时就不行。
这没什么大不了的,但我希望能够根据用户定义的配置以编程方式在字母列表中添加和删除。
我相信随着我深入了解Metaprogramming Elixir,我将能够解决这个问题,但如果有人有任何建议,我将不胜感激。
【问题讨论】:
-
我不知道这个问题的答案,但这可能会对您有所帮助:当您将参数传递给宏时,您传递的是 AST 表示。如果您传递
"string"或:atom,它们将作为文字传递,而当您传递变量时,它们将作为变量在 Elixir AST 中的表示方式传递:{:name, [line: 24], Elixir} -
@davoclavo:看起来这就是问题所在。制作了第二个使用列表/字典的宏,它工作得很好,除非(再次)列表是一个变量。将它作为变量传递给“(Protocol.UndefinedError)协议枚举未实现{:alphabets,[line:14],nil}”,所以看起来它得到了Macro.var的结果,我不是确定如何扩展为实际值,或者我什至应该这样做。