Skip to content

Commit dfb7dbe

Browse files
Make doctests from code texts for case/2 (#14423)
1 parent a47dc19 commit dfb7dbe

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

Diff for: lib/elixir/lib/kernel/special_forms.ex

+50-52
Original file line numberDiff line numberDiff line change
@@ -1914,33 +1914,31 @@ defmodule Kernel.SpecialForms do
19141914
19151915
## Examples
19161916
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"
19251925
19261926
In the example above, we match the result of `File.read/1`
19271927
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.
19291931
19301932
If no clause matches, an error is raised. For this reason,
19311933
it may be necessary to add a final catch-all clause (like `_`)
19321934
which will always match.
19331935
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)"
19441942
19451943
If you find yourself nesting `case` expressions inside
19461944
`case` expressions, consider using `with/1`.
@@ -1949,54 +1947,54 @@ defmodule Kernel.SpecialForms do
19491947
19501948
Note that variables bound in a clause do not leak to the outer context:
19511949
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
19561954
1957-
value
1958-
#=> unbound variable value
1955+
...> value
1956+
** (CompileError) undefined variable "value"
19591957
19601958
Variables in the outer context cannot be overridden either:
19611959
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
19711970
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.
19751975
19761976
If you want to pattern match against an existing variable,
19771977
you need to use the `^/1` operator:
19781978
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"
19861985
19871986
## Using guards to match against multiple values
19881987
19891988
While it is not possible to match against multiple patterns in a single
19901989
clause, it's possible to match against multiple values by using guards:
19911990
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"
20001998
"""
20011999
defmacro case(condition, clauses), do: error!([condition, clauses])
20022000

0 commit comments

Comments
 (0)