summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobb Shecter <robb@public.law>2019-02-13 23:53:44 -0800
committerAndrea Leopardi <an.leopardi@gmail.com>2019-02-14 08:53:44 +0100
commitddaa3f6ccb2c36117db141fc3b7c874213f62f6a (patch)
treecc9f8ca735719ee2f8d1037f5a19954a877467d8 /lib
parent58c86e1fcf41dd009eb99a2fc00d6d577510123d (diff)
downloadelixir-ddaa3f6ccb2c36117db141fc3b7c874213f62f6a.tar.gz
Focus on defguard in the "Guards" page (#8795)
The current doc first describes in detail the _deprecated_ way to create a custom guard. Only after that's complete is there a small note saying that this isn't recommended, and instead to use `defguard`. This commit changes that order around to lead with the current recommended practice.
Diffstat (limited to 'lib')
-rw-r--r--lib/elixir/pages/Guards.md16
1 files changed, 3 insertions, 13 deletions
diff --git a/lib/elixir/pages/Guards.md b/lib/elixir/pages/Guards.md
index f6786fdfc..6cafbd8ef 100644
--- a/lib/elixir/pages/Guards.md
+++ b/lib/elixir/pages/Guards.md
@@ -106,15 +106,11 @@ def my_function(number) when is_integer(number) and rem(number, 2) == 0 do
end
```
-This would be repetitive to write every time we need this check, so, as mentioned at the beginning of this section, we can abstract this away using a macro. Remember that defining a function that performs this check wouldn't work because we can't use custom functions in guards. Our macro would look like this:
+This would be repetitive to write every time we need this check, so, as mentioned at the beginning of this section, we can abstract this away using a macro. Remember that defining a function that performs this check wouldn't work because we can't use custom functions in guards. Use `defguard` and `defguardp` to create guard macros. Here's an example:
```elixir
defmodule MyInteger do
- defmacro is_even(number) do
- quote do
- is_integer(unquote(number)) and rem(unquote(number), 2) == 0
- end
- end
+ defguard is_even(value) when is_integer(value) and rem(value, 2) == 0
end
```
@@ -128,13 +124,7 @@ def my_function(number) when is_even(number) do
end
```
-While it's possible to create custom guards with macros, it's recommended to define them using `defguard` and `defguardp` which perform additional compile-time checks. Here's an example:
-
-```elixir
-defmodule MyInteger do
- defguard is_even(value) when is_integer(value) and rem(value, 2) == 0
-end
-```
+While it's possible to create custom guards with macros, it's recommended to define them using `defguard` and `defguardp` which perform additional compile-time checks.
## Multiple guards in the same clause