Skip to content

Commit cde4adf

Browse files
author
José Valim
committed
Since it is a common source of confusion, be explicit about the comma in blocks
1 parent a930cdb commit cde4adf

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,14 @@ iex> if true, do: 1 + 2
224224
3
225225
```
226226

227-
In Elixir, `do/end` blocks are a convenience for passing a group of expressions to `do:`. These are equivalent:
227+
Notice how the second example has a comma between `true` and `do:`, that's because it is using Elixir's regular syntax where each argument is separated by comma. We say this syntax is using **keyword lists**. We can pass `else` using keywords too:
228+
229+
```iex
230+
iex> if false, do: :this, else: :that
231+
:that
232+
```
233+
234+
`do/end` blocks are a syntatic convenience built on top of the keywords one. That's why `do/end` blocks do not require a comma between the previous argument the block. They are useful exactly because they remove the verbosity when writing blocks of code. These are equivalent:
228235

229236
```iex
230237
iex> if true do
@@ -239,13 +246,6 @@ iex> if true, do: (
239246
13
240247
```
241248

242-
We say the second syntax is using **keyword lists**. We can pass `else` using this syntax:
243-
244-
```iex
245-
iex> if false, do: :this, else: :that
246-
:that
247-
```
248-
249249
One thing to keep in mind when using `do/end` blocks is they are always bound to the outermost function call. For example, the following expression:
250250

251251
```iex

0 commit comments

Comments
 (0)