Skip to content

Commit 78eb14e

Browse files
author
José Valim
committed
Merge pull request #1187 from alco/pipeline-op
Pipeline op fix + additional assertion
2 parents a7d931c + 37c8867 commit 78eb14e

File tree

4 files changed

+183
-120
lines changed

4 files changed

+183
-120
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,7 @@ defmodule Kernel do
28352835
`|>` is called the pipeline operator as it is useful
28362836
to write pipeline style expressions. This operator
28372837
introduces the expression on the left as the first
2838-
argument to the expression on the right.
2838+
argument to the function call on the right.
28392839
28402840
## Examples
28412841
@@ -2846,8 +2846,8 @@ defmodule Kernel do
28462846
28472847
Enum.map(List.flatten([1,[2],3]), &1 * 2)
28482848
2849-
Please be aware of operator precendence, when using
2850-
this operator. For example, the following expression:
2849+
Be aware of operator precendence when using this operator.
2850+
For example, the following expression:
28512851
28522852
String.graphemes "Hello" |> Enum.reverse
28532853
@@ -2857,7 +2857,7 @@ defmodule Kernel do
28572857
28582858
Which will result in an error as Enumerable protocol
28592859
is not defined for binaries. Adding explicit parenthesis
2860-
is recommended:
2860+
resolves the ambiguity:
28612861
28622862
String.graphemes("Hello") |> Enum.reverse
28632863
@@ -2874,7 +2874,11 @@ defmodule Kernel do
28742874
{ call, line, [left] }
28752875
end
28762876

2877-
defp pipeline_op(left, { call, line, args }) when is_list(args) do
2877+
defp pipeline_op(left, { call, line, args }=right) when is_list(args) do
2878+
case validate_pipeline_args(args) do
2879+
:error -> pipeline_error(right)
2880+
_ -> nil
2881+
end
28782882
{ call, line, [left|args] }
28792883
end
28802884

@@ -2883,7 +2887,17 @@ defmodule Kernel do
28832887
end
28842888

28852889
defp pipeline_op(_, other) do
2886-
raise ArgumentError, message: "Unsupported expression in pipeline |> operator: #{inspect other}"
2890+
pipeline_error(other)
2891+
end
2892+
2893+
defp validate_pipeline_args([]), do: nil
2894+
defp validate_pipeline_args([ {:&,_,_ } | _ ]), do: :error
2895+
defp validate_pipeline_args([_|t]) do
2896+
validate_pipeline_args(t)
2897+
end
2898+
2899+
defp pipeline_error(arg) do
2900+
raise ArgumentError, message: "Unsupported expression in pipeline |> operator: #{Macro.to_binary arg}"
28872901
end
28882902

28892903
@doc """

0 commit comments

Comments
 (0)