Skip to content

Commit 13cab5f

Browse files
authored
Remove mentions to unless/2 in guides (#13852)
1 parent 619b1ee commit 13cab5f

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lib/elixir/pages/getting-started/case-cond-and-if.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,24 @@ iex> case :ok do
6767

6868
The documentation for the `Kernel` module lists all available guards in its sidebar. You can also consult the complete [Patterns and Guards](../references/patterns-and-guards.md#guards) reference for in-depth documentation.
6969

70-
## if/unless
70+
## if
7171

72-
`case` builds on pattern matching and guards to destructure and match on certain conditions. However, patterns and guards are limited only to certain expressions which are optimized by the compiler. In many situations, you need to write conditions that go beyond what can be expressed with `case`. For those, `if/2` (and `unless/2`) are useful alternatives:
72+
`case` builds on pattern matching and guards to destructure and match on certain conditions. However, patterns and guards are limited only to certain expressions which are optimized by the compiler. In many situations, you need to write conditions that go beyond what can be expressed with `case`. For those, `if/2` is a useful alternative:
7373

7474
```elixir
7575
iex> if true do
7676
...> "This works!"
7777
...> end
7878
"This works!"
79-
iex> unless true do
79+
iex> if false do
8080
...> "This will never be seen"
8181
...> end
8282
nil
8383
```
8484

85-
If the condition given to `if/2` returns `false` or `nil`, the body given between `do`-`end` is not executed and instead it returns `nil`. The opposite happens with `unless/2`.
85+
If the condition given to `if/2` returns `false` or `nil`, the body given between `do`-`end` is not executed and instead it returns `nil`.
8686

87-
They also support `else` blocks (although using `else` with `unless` is generally discouraged):
87+
`if/2` also supports `else` blocks:
8888

8989
```elixir
9090
iex> if nil do
@@ -121,9 +121,9 @@ iex> x = if true do
121121
2
122122
```
123123

124-
> #### `if` and `unless` are macros {: .info}
124+
> #### `if` is a macro {: .info}
125125
>
126-
> An interesting note regarding `if/2` and `unless/2` is that they are implemented as macros in the language: they aren't special language constructs as they would be in many languages. You can check the documentation and their source for more information.
126+
> An interesting note regarding `if/2` is that it is implemented as a macro in the language: it isn't a special language construct as it would be in many languages. You can check the documentation and its source for more information.
127127
128128
If you find yourself nesting several `if/2` blocks, you may want to consider using `cond/1` instead. Let's check it out.
129129

@@ -159,7 +159,7 @@ iex> cond do
159159
"This is always true (equivalent to else)"
160160
```
161161

162-
Similar to `if/2` and `unless/2`, `cond` considers any value besides `nil` and `false` to be true:
162+
Similar to `if/2`, `cond` considers any value besides `nil` and `false` to be true:
163163

164164
```elixir
165165
iex> cond do

lib/elixir/pages/meta-programming/macros.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ end
8484

8585
`Macro.expand_once/2` receives a quoted expression and expands it according to the current environment. In this case, it expanded/invoked the `Unless.macro_unless/2` macro and returned its result. We then proceeded to convert the returned quoted expression to a string and print it (we will talk about `__ENV__` later in this chapter).
8686

87-
That's what macros are all about. They are about receiving quoted expressions and transforming them into something else. In fact, `unless/2` in Elixir is implemented as a macro:
87+
That's what macros are all about. They are about receiving quoted expressions and transforming them into something else.
88+
In fact, `if/2` in Elixir is implemented as a macro:
8889

8990
```elixir
90-
defmacro unless(clause, do: expression) do
91+
defmacro if(clause, do: expression) do
9192
quote do
92-
if(!unquote(clause), do: unquote(expression))
93+
case clause do
94+
x when x in [false, nil] -> nil
95+
_ -> unquote(expression)
9396
end
9497
end
9598
```
9699

97-
Constructs such as `unless/2`, `defmacro/2`, `def/2`, `defprotocol/2`, and many others used throughout the Elixir standard library are written in pure Elixir, often as a macro. This means that the constructs being used to build the language can be used by developers to extend the language to the domains they are working on.
100+
Constructs such as `if/2`, `defmacro/2`, `def/2`, `defprotocol/2`, and many others used throughout the Elixir standard library are written in pure Elixir, often as a macro. This means that the constructs being used to build the language can be used by developers to extend the language to the domains they are working on.
98101

99102
We can define any function and macro we want, including ones that override the built-in definitions provided by Elixir. The only exceptions are Elixir special forms which are not implemented in Elixir and therefore cannot be overridden. The full list of special forms is available in `Kernel.SpecialForms`.
100103

0 commit comments

Comments
 (0)