@@ -503,7 +503,7 @@ defmodule Kernel.SpecialForms do
503503 * `:unquote` - When false, disables unquoting. Useful when you have a quote
504504 inside another quote and want to control what quote is
505505 able to unquote;
506- * `:location` - When set to `:keep`, keeps the current line and file on quotes .
506+ * `:location` - When set to `:keep`, keeps the current line and file from quote .
507507 Read the Stacktrace information section below for more information;
508508 * `:hygiene` - Allows a developer to disable hygiene selectively;
509509 * `:context` - Sets the resolution context;
@@ -721,35 +721,35 @@ defmodule Kernel.SpecialForms do
721721
722722 ## Stacktrace information
723723
724- One of Elixir's goals is to provide a proper stacktrace whenever there is an
725- exception. In order to work properly with macros, the default behavior
726- in quote is to not set a line. When a macro is invoked and the quoted
727- expressions is expanded, the call site line is inserted.
724+ When defining functions via macros, developers have the option of
725+ choosing if runtime errors will be reported from the caller or from
726+ inside the quote. Let's see an example:
728727
729- This is a good behavior for the majority of the cases, except if the macro
730- is defining new functions. Consider this example:
731-
732- defmodule MyServer do
733- use GenServer.Behaviour
728+ # adder.ex
729+ defmodule Adder do
730+ @doc "Defines a function that adds two numbers"
731+ defmacro defadd do
732+ quote location: :keep do
733+ def add(a, b), do: a + b
734+ end
735+ end
734736 end
735737
736- `GenServer.Behaviour` defines new functions in our `MyServer` module.
737- However, if there is an exception in any of these functions, we want
738- the stacktrace to point to the `GenServer.Behaviour` and not the line
739- that calls `use GenServer.Behaviour`. For this reason, there is an
740- option called `:location` that when set to `:keep` keeps the original
741- line and file lines instead of setting them to 0:
742-
743- quote location: :keep do
744- def handle_call(request, _from, state) do
745- { :reply, :undef, state }
746- end
738+ # sample.ex
739+ defmodule Sample do
740+ import Adder
741+ defadd
747742 end
748743
749- It is important to warn though that `location: :keep` evaluates the
750- code as if it was defined inside `GenServer.Behaviour` file, in
751- particular, the macro `__FILE__` and exceptions happening inside
752- the quote will always point to `GenServer.Behaviour` file.
744+ When using `location: :keep` and invalid arguments are given to
745+ `Sample.add/2`, the stacktrace information will point to the file
746+ and line inside the quote. Without `location: :keep`, the error is
747+ reported to where `defadd` was invoked.
748+
749+ Note that `location: :keep` evaluates the code as if it was defined
750+ inside the `Adder` file, in particular, the macro `__FILE__` and
751+ exceptions happening inside the quote will always point to the file
752+ where `Added` was defined.
753753
754754 ## Binding and unquote fragments
755755
0 commit comments