2017-03-05 118 views
1

我有一个语法错误,我不知道它来自哪里。这里是我的功能(在persona_from_auth.ex):与..do..else语句中的奇怪语法错误

# 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 

这将返回以下错误:

== 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 

第18行是创建()调用前行。

我检查了正确的灵药版本。在我有1.2的mix.exs中发生了,我将它改为1.4.2,但仍然是相同的错误。编译是否仍然使用1.2?我如何检查?

回答

2

with之后的第一条语句必须位于同一行上,或者参数需要放在括号中,否则Elixir会认为您试图拨打with/0,然后下面的行无效,导致语法错误。

下列任一应工作:

with {:notfound} <- check_github_email(auth.info.email), 
    {:notfound} <- check_google_email(auth.info.email)) 
do 
with(
    {:notfound} <- check_github_email(auth.info.email), 
    {:notfound} <- check_google_email(auth.info.email) 
) do 
+0

感谢。 'with'和'do'和'else'之间有一些主要区别吗?后者可以自行排队。 – raarts

+1

是的,“do”和“else”是语言关键字,而“with”不是。您不能创建名称为“do”或“else”的函数或变量。您可以在'iex'中尝试'do = 1','else = 1'和'with = 1'。只有'带'才能工作。 – Dogbert