@@ -1914,33 +1914,31 @@ defmodule Kernel.SpecialForms do
1914
1914
1915
1915
## Examples
1916
1916
1917
- case File.read( file) do
1918
- {:ok, contents} when is_binary(contents) ->
1919
- String.split( contents, "\n")
1920
-
1921
- {:error, _reason} ->
1922
- Logger.warning "could not find #{file}, assuming empty... "
1923
- []
1924
- end
1917
+ iex> file = "no_file.txt"
1918
+ iex> case File.read(file) do
1919
+ ...> {:ok, contents} when is_binary(contents) ->
1920
+ ...> String.split(contents, "\n")
1921
+ ...> {:error, _reason} ->
1922
+ ...> "Can't read the #{file} file "
1923
+ ...> end
1924
+ "Can't read the no_file.txt file"
1925
1925
1926
1926
In the example above, we match the result of `File.read/1`
1927
1927
against each clause "head" and execute the clause "body"
1928
- corresponding to the first clause that matches.
1928
+ corresponding to the first clause that matches. In our case
1929
+ there is no file, so `File.read/1` returns `{:error, :enoent}`
1930
+ and the second clause is matched.
1929
1931
1930
1932
If no clause matches, an error is raised. For this reason,
1931
1933
it may be necessary to add a final catch-all clause (like `_`)
1932
1934
which will always match.
1933
1935
1934
- x = 10
1935
-
1936
- case x do
1937
- 0 ->
1938
- "This clause won't match"
1939
-
1940
- _ ->
1941
- "This clause would match any value (x = #{x})"
1942
- end
1943
- #=> "This clause would match any value (x = 10)"
1936
+ iex> x = 10
1937
+ iex> case x do
1938
+ ...> 0 -> "This clause won't match"
1939
+ ...> _ -> "This clause would match any value (x = #{x})"
1940
+ ...> end
1941
+ "This clause would match any value (x = 10)"
1944
1942
1945
1943
If you find yourself nesting `case` expressions inside
1946
1944
`case` expressions, consider using `with/1`.
@@ -1949,54 +1947,54 @@ defmodule Kernel.SpecialForms do
1949
1947
1950
1948
Note that variables bound in a clause do not leak to the outer context:
1951
1949
1952
- case data do
1953
- {:ok, value} -> value
1954
- :error -> nil
1955
- end
1950
+ iex> case {:ok, 7} do
1951
+ ...> {:ok, value} -> value
1952
+ ...> :error -> nil
1953
+ ...> end
1956
1954
1957
- value
1958
- #=> unbound variable value
1955
+ ...> value
1956
+ ** (CompileError) undefined variable " value"
1959
1957
1960
1958
Variables in the outer context cannot be overridden either:
1961
1959
1962
- value = 7
1963
-
1964
- case lucky? do
1965
- false -> value = 13
1966
- true -> true
1967
- end
1968
-
1969
- value
1970
- #=> 7
1960
+ iex> value = 7
1961
+ iex> case 3 > 5 do
1962
+ ...> false ->
1963
+ ...> value = 3
1964
+ ...> value + 2
1965
+ ...> true ->
1966
+ ...> 3
1967
+ ...> end
1968
+ iex> value
1969
+ 7
1971
1970
1972
- In the example above, `value` is going to be `7` regardless of the value of
1973
- `lucky?`. The variable `value` bound in the clause and the variable `value`
1974
- bound in the outer context are two entirely separate variables.
1971
+ In the example above, `value` is going to be `7` regardless of
1972
+ which clause matched. The variable `value` bound in the clause
1973
+ and the variable `value` bound in the outer context are two
1974
+ entirely separate variables.
1975
1975
1976
1976
If you want to pattern match against an existing variable,
1977
1977
you need to use the `^/1` operator:
1978
1978
1979
- x = 1
1980
-
1981
- case 10 do
1982
- ^x -> "Won't match"
1983
- _ -> "Will match"
1984
- end
1985
- #=> "Will match"
1979
+ iex> x = 1
1980
+ iex> case 10 do
1981
+ ...> ^x -> "Won't match"
1982
+ ...> _ -> "Will match"
1983
+ ...> end
1984
+ "Will match"
1986
1985
1987
1986
## Using guards to match against multiple values
1988
1987
1989
1988
While it is not possible to match against multiple patterns in a single
1990
1989
clause, it's possible to match against multiple values by using guards:
1991
1990
1992
- case data do
1993
- value when value in [:one, :two] ->
1994
- "#{value} has been matched"
1995
-
1996
- :three ->
1997
- "three has been matched"
1998
- end
1999
-
1991
+ iex> case :two do
1992
+ ...> value when value in [:one, :two] ->
1993
+ ...> "#{value} has been matched"
1994
+ ...> :three ->
1995
+ ...> "three has been matched"
1996
+ ...> end
1997
+ "two has been matched"
2000
1998
"""
2001
1999
defmacro case ( condition , clauses ) , do: error! ( [ condition , clauses ] )
2002
2000
0 commit comments