From 8047a9bcad16b8e1f43d1d9d33fac09d8adea865 Mon Sep 17 00:00:00 2001 From: tomtuamnuq <76523782+tomtuamnuq@users.noreply.github.com> Date: Fri, 14 Jan 2022 09:43:14 +0100 Subject: [PATCH] Add description of local kw in global scope (#42794) --- doc/src/manual/variables-and-scoping.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/src/manual/variables-and-scoping.md b/doc/src/manual/variables-and-scoping.md index 8df036f8c06aae..82eb5cc256d245 100644 --- a/doc/src/manual/variables-and-scoping.md +++ b/doc/src/manual/variables-and-scoping.md @@ -99,6 +99,22 @@ julia> module E ERROR: cannot assign variables in other modules ``` +If a top-level expression contains a variable declaration with keyword `local`, +then that variable is not accessible outside that expression. +The variable inside the expression does not affect global variables of the same name. +An example is to declare `local x` in a `begin` or `if` block at the top-level: + +```jldoctest +julia> x = 1 + begin + local x = 0 + @show x + end + @show x; +x = 0 +x = 1 +``` + Note that the interactive prompt (aka REPL) is in the global scope of the module `Main`. ## Local Scope