..do..else语句中出现奇怪的语法错误

raa*_*rts 4 elixir phoenix-framework

我有一个语法错误,我不知道它从哪里来。这是我的功能(在persona_from_auth.ex中):

  # find or create the user.
  # if you login with oauth2, your account will auto created
  def find_or_create(%Auth{provider: :github} = auth) do
    with
      {:notfound} <- check_github_email(auth.info.email),
      {:notfound} <- check_google_email(auth.info.email)
    do
        create(auth)
    else
      {:ok, persona} -> update(auth, persona)
    end
  end
Run Code Online (Sandbox Code Playgroud)

这将返回以下错误:

== Compilation error on file web/models/persona_from_auth.ex ==
** (SyntaxError) web/models/persona_from_auth.ex:18: syntax error before: do
    (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
Run Code Online (Sandbox Code Playgroud)

第18行是create()调用之前的行。

我检查了正确的长生不老药版本。在mix.exs中发现我有1.2,我将其更改为1.4.2,但仍然是相同的错误。编译是否仍可能使用1.2?我该如何检查?

Dog*_*ert 5

之后的第一条语句with必须在同一行上,或者参数必须在括号中,否则Elixir认为您正在尝试调用with/0,然后以下几行没有意义,从而导致语法错误。

以下任何一种都可以工作:

with {:notfound} <- check_github_email(auth.info.email),
     {:notfound} <- check_google_email(auth.info.email))
do
Run Code Online (Sandbox Code Playgroud)
with(
  {:notfound} <- check_github_email(auth.info.email),
  {:notfound} <- check_google_email(auth.info.email)
) do
Run Code Online (Sandbox Code Playgroud)

  • 是的,“ do”和“ else”是语言关键字,而“ with”则不是。您不能使用名称“ do”或“ else”创建函数或变量。您可以在“ iex”中尝试“ do = 1”,“ else = 1”和“ with = 1”。只有`with`起作用。 (2认同)