You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/pages/getting-started/case-cond-and-if.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -67,24 +67,24 @@ iex> case :ok do
67
67
68
68
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.
69
69
70
-
## if/unless
70
+
## if
71
71
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:
73
73
74
74
```elixir
75
75
iex>iftruedo
76
76
...>"This works!"
77
77
...>end
78
78
"This works!"
79
-
iex>unlesstruedo
79
+
iex>iffalsedo
80
80
...>"This will never be seen"
81
81
...>end
82
82
nil
83
83
```
84
84
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`.
86
86
87
-
They also support `else` blocks (although using `else`with `unless` is generally discouraged):
87
+
`if/2` also supports `else`blocks:
88
88
89
89
```elixir
90
90
iex>ifnildo
@@ -121,9 +121,9 @@ iex> x = if true do
121
121
2
122
122
```
123
123
124
-
> #### `if`and `unless` are macros {: .info}
124
+
> #### `if`is a macro {: .info}
125
125
>
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.
127
127
128
128
If you find yourself nesting several `if/2` blocks, you may want to consider using `cond/1` instead. Let's check it out.
129
129
@@ -159,7 +159,7 @@ iex> cond do
159
159
"This is always true (equivalent to else)"
160
160
```
161
161
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:
Copy file name to clipboardExpand all lines: lib/elixir/pages/meta-programming/macros.md
+7-4
Original file line number
Diff line number
Diff line change
@@ -84,17 +84,20 @@ end
84
84
85
85
`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).
86
86
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:
88
89
89
90
```elixir
90
-
defmacrounless(clause, do: expression) do
91
+
defmacroif(clause, do: expression) do
91
92
quotedo
92
-
if(!unquote(clause), do:unquote(expression))
93
+
case clause do
94
+
x when x in [false, nil] ->nil
95
+
_->unquote(expression)
93
96
end
94
97
end
95
98
```
96
99
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.
98
101
99
102
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`.
0 commit comments