From 580c414a1f7be6e188c22636c465fea1dbf390c9 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Sat, 26 Apr 2025 09:03:29 +0900 Subject: [PATCH 1/6] Rework if/2 docs --- lib/elixir/lib/kernel.ex | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index bd6a4e6202..284bf82b36 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3898,48 +3898,40 @@ defmodule Kernel do ## One-liner examples - if(foo, do: bar) + iex> if 7 > 5, do: "yes" + "yes" In the example above, `bar` will be returned if `foo` evaluates to a truthy value (neither `false` nor `nil`). Otherwise, `nil` will be returned. + iex> if 3 > 5, do: "yes" + nil + An `else` option can be given to specify the opposite: - if(foo, do: bar, else: baz) + iex> if 3 > 5, do: "yes", else: "no" + "no" ## Blocks examples It's also possible to pass a block to the `if/2` macro. The first example above would be translated to: - if foo do - bar - end + iex> if 7 > 5 do + ...> "yes" + ...> end + "yes" Note that `do`-`end` become delimiters. The second example would translate to: - if foo do - bar - else - baz - end - - ## Examples - - iex> if 5 > 7 do - ...> "This will never be returned" + iex> if 3 > 5 do + ...> "yes" ...> else - ...> "This will be returned" + ...> "no" ...> end - "This will be returned" - - iex> if 2 + 2 == 4, do: :correct - :correct - - iex> if 2 + 2 == 5, do: :correct - nil + "no" If you find yourself nesting conditionals inside conditionals, consider using `cond/1`. From a4d1af057f058ecbe9701fb57555c9f722572276 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Sat, 26 Apr 2025 09:13:38 +0900 Subject: [PATCH 2/6] Add section about variable scope --- lib/elixir/lib/kernel.ex | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index 284bf82b36..fb15aa0acc 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3935,6 +3935,26 @@ defmodule Kernel do If you find yourself nesting conditionals inside conditionals, consider using `cond/1`. + + ## Variables scope + + Variables can be defined or rebound in `do`/`else` blocks, but these will not leak to the outer context: + + x = 1 + if x > 0 do + x = x + 1 + IO.puts(x) # prints 2 + end + x # 1 + + Variables can be defined in the condition as well, but they are available in the outer context: + + fruits = %{oranges: 3} + if count = fruits[:apples] do + IO.puts(count + 1) + end + counts # nil + """ defmacro if(condition, clauses) do build_if(condition, clauses) From a18499803ed7b2474ea46be72ee383371ed90e77 Mon Sep 17 00:00:00 2001 From: Jean Klingler Date: Sat, 26 Apr 2025 10:09:31 +0900 Subject: [PATCH 3/6] Fix typo in variable name Co-authored-by: Parker Selbert --- lib/elixir/lib/kernel.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index fb15aa0acc..b6aad38909 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3953,7 +3953,7 @@ defmodule Kernel do if count = fruits[:apples] do IO.puts(count + 1) end - counts # nil + count # nil """ defmacro if(condition, clauses) do From 96bbccffc386a0728686c840d99e04ef964a9a48 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Sat, 26 Apr 2025 15:11:08 +0900 Subject: [PATCH 4/6] Fix description and add truthy/falsy examples --- lib/elixir/lib/kernel.ex | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index b6aad38909..4a8968dc40 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3901,13 +3901,19 @@ defmodule Kernel do iex> if 7 > 5, do: "yes" "yes" - In the example above, `bar` will be returned if `foo` evaluates to - a truthy value (neither `false` nor `nil`). Otherwise, `nil` will be - returned. + iex> if "truthy value", do: "yes" + "yes" + + In the examples above, `"yes"` will be returned because the condition + evaluates to a truthy value (neither `false` nor `nil`). Otherwise, `nil` + will be returned: iex> if 3 > 5, do: "yes" nil + iex> if nil, do: "yes" + nil + An `else` option can be given to specify the opposite: iex> if 3 > 5, do: "yes", else: "no" From 6d562878ed44bcc1dbee0843fd38dcd49b923edd Mon Sep 17 00:00:00 2001 From: sabiwara Date: Sat, 26 Apr 2025 15:20:44 +0900 Subject: [PATCH 5/6] More edits --- lib/elixir/lib/kernel.ex | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index 4a8968dc40..c786bab331 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3904,14 +3904,14 @@ defmodule Kernel do iex> if "truthy value", do: "yes" "yes" - In the examples above, `"yes"` will be returned because the condition - evaluates to a truthy value (neither `false` nor `nil`). Otherwise, `nil` - will be returned: + In the examples above, the `do` clause is evaluated and `"yes"` will be returned + because the condition evaluates to a truthy value (neither `false` nor `nil`). + Otherwise, the clause is not evaluated and `nil` will be returned: iex> if 3 > 5, do: "yes" nil - iex> if nil, do: "yes" + iex> if nil, do: IO.puts("this won't be printed") nil An `else` option can be given to specify the opposite: @@ -3929,7 +3929,7 @@ defmodule Kernel do ...> end "yes" - Note that `do`-`end` become delimiters. The second example would + Note that `do`-`end` become delimiters. The third example would translate to: iex> if 3 > 5 do @@ -3957,6 +3957,7 @@ defmodule Kernel do fruits = %{oranges: 3} if count = fruits[:apples] do + # won't be evaluated IO.puts(count + 1) end count # nil From d1bb467be53ffb0599616b04ce6a4d8bc868460f Mon Sep 17 00:00:00 2001 From: Jean Klingler Date: Sat, 26 Apr 2025 16:21:09 +0900 Subject: [PATCH 6/6] Use clearer language MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/elixir/lib/kernel.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index c786bab331..df942bb21d 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -3944,7 +3944,7 @@ defmodule Kernel do ## Variables scope - Variables can be defined or rebound in `do`/`else` blocks, but these will not leak to the outer context: + Variables set within `do`/`else` blocks do not leak to the outer context: x = 1 if x > 0 do @@ -3953,7 +3953,7 @@ defmodule Kernel do end x # 1 - Variables can be defined in the condition as well, but they are available in the outer context: + Variables set in the condition are available in the outer context: fruits = %{oranges: 3} if count = fruits[:apples] do