From 2f6e2e2cd693e5c94a305aaf059f7844bb8911ef Mon Sep 17 00:00:00 2001 From: Henrique Becker Date: Mon, 8 Mar 2021 11:26:22 -0300 Subject: [PATCH 1/2] Write down what we know about @Base.error --- base/expr.jl | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/base/expr.jl b/base/expr.jl index 38a1c4e7089cb..546e981e4f93c 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -230,12 +230,22 @@ end `@pure` gives the compiler a hint for the definition of a pure function, helping for type inference. -A pure function can only depend on immutable information. -This also means a `@pure` function cannot use any global mutable state, including -generic functions. Calls to generic functions depend on method tables which are +The criteria used by Julia to deem a function pure is stricter than the one +used by most other languages, and incorrect `@pure` annotation may introduce +hard to identify bugs, so it is important to keep in mind these criteria: + +1. A pure function must always return exactly (`===`) the same result for a given input. + If the return is a mutable struct this means it must always return the *same* object. +2. A pure function cannot be extended with new methods after it is called the first time. +3. A pure function cannot recurse (i.e., call itself). +4. A pure function should only call built-in functions, no generic functions. + If you write the name of a function in the REPL and press enter it will inform + if the function is generic or builtin. + +The rationale for 2--4 comes comes the fact that a `@pure` function cannot use any +global mutable state. Calls to generic functions depend on method tables which are mutable global state. -Use with caution, incorrect `@pure` annotation of a function may introduce -hard to identify bugs. Double check for calls to generic functions. + This macro is intended for internal compiler use and may be subject to changes. """ macro pure(ex) From bf3fcd6906d75179e96ddc263d20081e50622ab6 Mon Sep 17 00:00:00 2001 From: Henrique Becker Date: Mon, 8 Mar 2021 14:46:42 -0300 Subject: [PATCH 2/2] Apply changes mentioned by @vtjnash. * Removed (at least for now) the mention about recursion. If we are sure it is only allowed in some specific circumstances we should add it back. * Stopped comparing Julia criteria with other languages. --- base/expr.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/base/expr.jl b/base/expr.jl index 546e981e4f93c..d6fdb6db82e54 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -230,19 +230,18 @@ end `@pure` gives the compiler a hint for the definition of a pure function, helping for type inference. -The criteria used by Julia to deem a function pure is stricter than the one -used by most other languages, and incorrect `@pure` annotation may introduce -hard to identify bugs, so it is important to keep in mind these criteria: +The criteria used by Julia to deem a function pure is very strict, +and incorrect `@pure` annotation may introduce hard to identify bugs, +so it is important to keep in mind these criteria: 1. A pure function must always return exactly (`===`) the same result for a given input. If the return is a mutable struct this means it must always return the *same* object. 2. A pure function cannot be extended with new methods after it is called the first time. -3. A pure function cannot recurse (i.e., call itself). -4. A pure function should only call built-in functions, no generic functions. +3. A pure function should only call built-in functions, no generic functions. If you write the name of a function in the REPL and press enter it will inform if the function is generic or builtin. -The rationale for 2--4 comes comes the fact that a `@pure` function cannot use any +The rationale for 2 and 3 comes comes the fact that a `@pure` function cannot use any global mutable state. Calls to generic functions depend on method tables which are mutable global state.