2016-05-15 33 views
3

我無法理解爲什麼這個工程:藥劑管無效語法

1..1000 |> Stream.map(&(3 * &1)) |> Enum.sum 

雖然這並不:

​​

唯一的區別是.map 我的理解後的空間,應靈藥在這種情況下不關心空白。

運行上面的代碼中iex產生以下錯誤:

warning: you are piping into a function call without parentheses, which may be ambiguous. Please wrap the function you are piping into in parentheses. For example: 

foo 1 |> bar 2 |> baz 3 

Should be written as: 
** (FunctionClauseError) no function clause matching in Enumerable.Function.reduce/3 

foo(1) |> bar(2) |> baz(3) 

(elixir) lib/enum.ex:2776: Enumerable.Function.reduce(#Function<6.54118792/1 in :erl_eval.expr/5>, {:cont, 0}, #Function<44.12486327/2 in Enum.reduce/3>) 
(elixir) lib/enum.ex:1486: Enum.reduce/3 

爲什麼管操作使得這裏的兩種情況之間的區別?

回答

9

的空白改變的優先級是如何解決:

iex(4)> quote(do: Stream.map(1) |> Enum.sum) |> Macro.to_string 
"Stream.map(1) |> Enum.sum()" 

iex(5)> quote(do: Stream.map (1) |> Enum.sum) |> Macro.to_string 
"Stream.map(1 |> Enum.sum())" 

而且藥劑不支持保留功能和括號之間的空間 - 它只能由意外的一元函數,因爲括號是可選的:foo (1)是與foo((1))相同。你可以看到它不支持與更多參數的功能:

iex(2)> quote(do: foo (4, 5)) 
** (SyntaxError) iex:2: unexpected parentheses. 
If you are making a function call, do not insert spaces between 
the function name and the opening parentheses. 
Syntax error before: '(' 
+0

oohhh,謝謝,完全沒有想到會是這樣:)非常好的解釋! –