Skip to content

Commit d769f8f

Browse files
author
José Valim
committed
Improve docs for case, try, receive and ^
1 parent 9cd5680 commit d769f8f

File tree

1 file changed

+66
-26
lines changed

1 file changed

+66
-26
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,30 @@ defmodule Kernel.SpecialForms do
484484
"""
485485
defmacro __DIR__
486486

487+
@doc """
488+
Access an already bound variable in match clauses.
489+
490+
## Examples
491+
492+
Elixir allows variables to be rebound via static single assignment:
493+
494+
iex> x = 1
495+
iex> x = 2
496+
iex> x
497+
2
498+
499+
However, in some situations, it is useful to match against an existing
500+
value, instead of rebinding. This can be done with the `^` special form:
501+
502+
iex> x = 1
503+
iex> ^x = 1
504+
iex> ^x = 2
505+
** (MatchError) no match of right hand side value: 2
506+
507+
Note the `^` special form is only useful in matches.
508+
"""
509+
defmacro ^(var)
510+
487511
@doc %S"""
488512
Gets the representation of any expression.
489513
@@ -1201,44 +1225,54 @@ defmodule Kernel.SpecialForms do
12011225
defmacro super(args)
12021226

12031227
@doc """
1204-
Matches the given expression against the match clauses.
1228+
Matches the given expression against the given clauses.
12051229
12061230
## Examples
12071231
12081232
case thing do
12091233
{ :selector, i, value } when is_integer(i) ->
12101234
value
1211-
value -> value
1235+
value ->
1236+
value
12121237
end
12131238
1214-
In the example above, we compare `thing` with each given match
1215-
clause and evaluate the expression corresponding to the first clause
1239+
In the example above, we match `thing` against each clause "head"
1240+
and execute the clause "body" corresponding to the first clause
12161241
that matches. If no clause matches, an error is raised.
12171242
1218-
Since Elixir variables can be assigned more than once, variables
1219-
in a match clause will always be assigned instead of matching with
1220-
its previous values. For example:
1243+
## Variables handling
1244+
1245+
Notice that variables bound in a clause "head" do not leak to the
1246+
outer context:
12211247
1222-
i = 1
1223-
case 10 do
1224-
i -> i * 2
1248+
case data do
1249+
{ :ok, value } -> value
1250+
:error -> nil
12251251
end
12261252
1227-
The example above will return 20, because `i` is assigned to 10
1228-
and then multiplied by 2. If you desire to match the value of `i`
1229-
against the given condition, you need to use the `^` operator:
1253+
value #=> unbound variable value
1254+
1255+
However, variables explicitly bound in the clause "body" are
1256+
accessible from the outer context:
1257+
1258+
value = 7
12301259
1231-
i = 1
1232-
case 10 do
1233-
^i -> i * 2
1260+
case lucky? do
1261+
false -> value = 13
1262+
true -> true
12341263
end
12351264
1236-
The example above will actually fail because 10 does not match 1.
1265+
value #=> 7 or 13
1266+
1267+
In the example above, value is going to be `7` or `13` depending on
1268+
the value of `lucky?`. In case `value` has no previous value before
1269+
case, clauses that do not explicitly bind a value have the variable
1270+
bound to nil.
12371271
"""
12381272
defmacro case(condition, blocks)
12391273

12401274
@doc """
1241-
Evaluate the given expressions and catch any error, exit
1275+
Evaluate the given expressions and handle any error, exit
12421276
or throw that may have happened.
12431277
12441278
## Examples
@@ -1285,14 +1319,14 @@ defmodule Kernel.SpecialForms do
12851319
[UndefinedFunctionError] -> nil
12861320
end
12871321
1288-
# rescue and assign to x
1322+
# rescue and bind to x
12891323
try do
12901324
UndefinedModule.undefined_function
12911325
rescue
12921326
x in [UndefinedFunctionError] -> nil
12931327
end
12941328
1295-
# rescue all and assign to x
1329+
# rescue all and bind to x
12961330
try do
12971331
UndefinedModule.undefined_function
12981332
rescue
@@ -1397,7 +1431,7 @@ defmodule Kernel.SpecialForms do
13971431
with a tail call as the final call inside an else clause. The same
13981432
is true for rescue and catch clauses.
13991433
1400-
## Variable visibility
1434+
## Variable handling
14011435
14021436
Since an expression inside `try` may not have been evaluated
14031437
due to an exception, any variable created inside `try` cannot
@@ -1411,7 +1445,7 @@ defmodule Kernel.SpecialForms do
14111445
_, _ -> :failed
14121446
end
14131447
1414-
x #=> Cannot access `x`
1448+
x #=> unbound variable `x`
14151449
14161450
In the example above, `x` cannot be accessed since it was defined
14171451
inside the `try` clause. A common practice to address this issue
@@ -1430,8 +1464,11 @@ defmodule Kernel.SpecialForms do
14301464
defmacro try(args)
14311465

14321466
@doc """
1433-
The current process will hang until it receives a message
1434-
from other processes that matches the given clauses.
1467+
Checks if there is a message matching the given clauses
1468+
in the current process mailbox.
1469+
1470+
In case there is no such message, the current process hangs
1471+
until a message arrives or waits until a given timeout value.
14351472
14361473
## Examples
14371474
@@ -1444,8 +1481,6 @@ defmodule Kernel.SpecialForms do
14441481
IO.puts :stderr, "Unexpected message received"
14451482
end
14461483
1447-
The match clauses above follows the same rules as `case/2`.
1448-
14491484
An optional after clause can be given in case the message was not
14501485
received after the specified period of time:
14511486
@@ -1469,6 +1504,11 @@ defmodule Kernel.SpecialForms do
14691504
14701505
* 0 - if there is no matching message in the mailbox, the timeout
14711506
will occur immediately.
1507+
1508+
## Variables handling
1509+
1510+
The `receive` special form handles variables exactly as the `case`
1511+
special macro. For more information, check the docs for `case/2`.
14721512
"""
14731513
defmacro receive(args)
14741514
end

0 commit comments

Comments
 (0)