@@ -1914,20 +1914,19 @@ defmodule Kernel.SpecialForms do
1914
1914
1915
1915
## Examples
1916
1916
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"
1917
+ iex> string_date = "2015-01-23"
1918
+ iex> case Date.from_iso8601(string_date) do
1919
+ ...> {:ok, date} -> date
1920
+ ...> {:error, _reason} -> Date.utc_today()
1923
1921
...> end
1924
- "Can't read the no_file.txt file"
1922
+ ~D[2015-01-23]
1925
1923
1926
- In the example above, we match the result of `File.read /1`
1924
+ In the example above, we match the result of `Date.from_iso8601 /1`
1927
1925
against each clause "head" and execute the clause "body"
1928
1926
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.
1927
+ `string_date` contains a string with a valid ISO 8601 representation
1928
+ of date. The function returns `{:ok, ~D[2015-01-23]}`, so the
1929
+ `{:ok, date}` clause is matched.
1931
1930
1932
1931
If no clause matches, an error is raised. For this reason,
1933
1932
it may be necessary to add a final catch-all clause (like `_`)
0 commit comments