Skip to content

Commit 7f81b9b

Browse files
committed
Add outputs
1 parent ce32e19 commit 7f81b9b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

getting-started/case-cond-and-if.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ iex> case {1, 2, 3} do
2323
...> _ ->
2424
...> "This clause would match any value"
2525
...> end
26+
"This clause will match and bind x to 2 in this clause"
2627
```
2728

2829
If you want to pattern match against an existing variable, you need to use the `^` operator:
@@ -34,6 +35,7 @@ iex> case 10 do
3435
...> ^x -> "Won't match"
3536
...> _ -> "Will match"
3637
...> end
38+
"Will match"
3739
```
3840

3941
Clauses also allow extra conditions to be specified via guards:
@@ -45,6 +47,7 @@ iex> case {1, 2, 3} do
4547
...> _ ->
4648
...> "Won't match"
4749
...> end
50+
"Will match"
4851
```
4952

5053
The first clause above will only match when `x` is positive.
@@ -162,6 +165,7 @@ iex> cond do
162165
...> true ->
163166
...> "This is always true (equivalent to else)"
164167
...> end
168+
"This is always true (equivalent to else)"
165169
```
166170

167171
Finally, note `cond` considers any value besides `nil` and `false` to be true:
@@ -241,6 +245,7 @@ One thing to keep in mind when using `do/end` blocks is they are always bound to
241245
iex> is_number if true do
242246
...> 1 + 2
243247
...> end
248+
** (RuntimeError) undefined function: if/1
244249
```
245250

246251
Would be parsed as:
@@ -249,6 +254,7 @@ Would be parsed as:
249254
iex> is_number(if true) do
250255
...> 1 + 2
251256
...> end
257+
** (RuntimeError) undefined function: if/1
252258
```
253259

254260
Which leads to an undefined function error as Elixir attempts to invoke `is_number/2`. Adding explicit parentheses is enough to resolve the ambiguity:

0 commit comments

Comments
 (0)