# \#scope

**URL:** https://discourse.julialang.org/tag/scope/198.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [The while cycle doesn't work](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542)

<div class="topic-metadata">

**Author:** [@Kobold](https://discourse.julialang.org/u/Kobold)\
**Replies:** 3\
**Last updated:** [December 16, 2025, 8:29pm UTC](https://discourse.julialang.org/t/the-while-cycle-doesnt-work/134542 "2025-12-16T20:29:35Z")

</div>

I wrote a simple program, literally to test how the while cycle works, but it gives the same error, even though I wrote it identically to the one in the training material. x=0 while x\<10 x+=1 println(x) end and the…

---

## [Remove the soft scope altogether and make it global](https://discourse.julialang.org/t/remove-the-soft-scope-altogether-and-make-it-global/33333)

<div class="topic-metadata">

**Author:** [@Wenjie](https://discourse.julialang.org/u/Wenjie)\
**Replies:** 49\
**Last updated:** [May 17, 2025, 5:03am UTC](https://discourse.julialang.org/t/remove-the-soft-scope-altogether-and-make-it-global/33333 "2025-05-17T05:03:15Z")

</div>

I see that the Julia community are currently debating on the new scope rules in v1.0, in an attempt to return to the deprecated soft/hard local scopes in v0.6. In my opinion, neither solution is ideal. Here, let me give …

---

## [Scope in functions](https://discourse.julialang.org/t/scope-in-functions/22096)

<div class="topic-metadata">

**Author:** [@alee](https://discourse.julialang.org/u/alee)\
**Replies:** 13\
**Last updated:** [May 17, 2025, 4:28am UTC](https://discourse.julialang.org/t/scope-in-functions/22096 "2025-05-17T04:28:40Z")

</div>

One of my collaborators found the following behaviour confusing. function foo() function bar() x = 3 return x end x = 1 bar() return x end foo() which outputs 3 (and not 1). This behaviour is differe…

---

## [Global scope confusion again](https://discourse.julialang.org/t/global-scope-confusion-again/20044)

<div class="topic-metadata">

**Author:** [@Paul](https://discourse.julialang.org/u/Paul)\
**Replies:** 52\
**Last updated:** [May 17, 2025, 4:13am UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044 "2025-05-17T04:13:28Z")

</div>

This code runs in JuliaBox (0.6.4), but running the code in JuliaPro on OSX Mojave the same code (cut and pasted) returns an error message (undefined variable a). I have the same problem both on my MacMini and my MacBook…

---

## [Can anyone improve this macro?](https://discourse.julialang.org/t/can-anyone-improve-this-macro/127359)

<div class="topic-metadata">

**Author:** [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Replies:** 3\
**Last updated:** [March 26, 2025, 4:05am UTC](https://discourse.julialang.org/t/can-anyone-improve-this-macro/127359 "2025-03-26T04:05:37Z")

</div>

I have written the following macro: using Logging """ @no\_logging(code\_to\_execute) Execute \`code\_to\_execute\` sending all logging to the \[\`Logging.NullLogger\`\](@ref). """ macro no\_logging(code) return :( with\_…

---

## [Variable scope concept](https://discourse.julialang.org/t/variable-scope-concept/122277)

<div class="topic-metadata">

**Author:** [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Replies:** 21\
**Last updated:** [November 16, 2024, 1:03pm UTC](https://discourse.julialang.org/t/variable-scope-concept/122277 "2024-11-16T13:03:31Z")

</div>

Hello, Why is the error related to the variable scope given in the following code? num = 0 for line in eachline("file.txt") num +=1 end print(num) The error is: ┌ Warning: Assignment to \`num\` in soft scope is am…

---

## [Using Let block](https://discourse.julialang.org/t/using-let-block/110490)

<div class="topic-metadata">

**Author:** [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Replies:** 15\
**Last updated:** [November 2, 2024, 6:33pm UTC](https://discourse.julialang.org/t/using-let-block/110490 "2024-11-02T18:33:41Z")

</div>

If I define a variable b in Main and then use the let block: b = 5 let b = b println(b) end I get the error saying: " UndefVarError: b not defined" I can get this to work by doing: b = 5 let b = Main.b prin…

---

## [The manual's section on variable scope sucks](https://discourse.julialang.org/t/the-manuals-section-on-variable-scope-sucks/119522)

<div class="topic-metadata">

**Author:** [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Replies:** 46\
**Last updated:** [October 19, 2024, 6:35am UTC](https://discourse.julialang.org/t/the-manuals-section-on-variable-scope-sucks/119522 "2024-10-19T06:35:14Z")

</div>

I know it’s not for the lack of trying, but the manual’s section on scope is unclear if not outright wrong. Issue 1 The manual says: If you assign to an existing local, it always \[emphasis not mine\] updates that exis…

---

## [Unexpected result from setprecision](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336)

<div class="topic-metadata">

**Author:** [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Replies:** 3\
**Last updated:** [October 18, 2024, 1:55am UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336 "2024-10-18T01:55:09Z")

</div>

I expected the following to return 512, rather than 256. Could someone please explain why this is happening? julia\> setprecision(512) do thisbig = big"1.0" @show precision(thisbig) end prec…

---

## [Soft scopes & globals in \`@testitem\`s](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338)

<div class="topic-metadata">

**Author:** [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Replies:** 7\
**Last updated:** [September 25, 2024, 11:07am UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338 "2024-09-25T11:07:54Z")

</div>

I just encountered the problem discussed in Best solution to Julia's soft scope problem? , but in the context of unit tests. Specifically, in code like this: @testitem "foo" begin A = 0 for i in 1:10 A …

---

## [Is there a way to enable non-interactive scope behaviors in REPL?](https://discourse.julialang.org/t/is-there-a-way-to-enable-non-interactive-scope-behaviors-in-repl/118916)

<div class="topic-metadata">

**Author:** [@purpletree](https://discourse.julialang.org/u/purpletree)\
**Replies:** 13\
**Last updated:** [September 2, 2024, 9:31pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enable-non-interactive-scope-behaviors-in-repl/118916 "2024-09-02T21:31:43Z")

</div>

I am going to be creating my first project in Julia by writing and debugging in interactive sessions, but once I have it working the final code will run non-interactively on a HPC cluster. I have learned the basics of J…

---

## [Clarification on the scope of import/using/include](https://discourse.julialang.org/t/clarification-on-the-scope-of-import-using-include/117222)

<div class="topic-metadata">

**Author:** [@Zotnam](https://discourse.julialang.org/u/Zotnam)\
**Replies:** 1\
**Last updated:** [July 19, 2024, 9:44am UTC](https://discourse.julialang.org/t/clarification-on-the-scope-of-import-using-include/117222 "2024-07-19T09:44:49Z")

</div>

Hi guys, i was trying to understand the source code of the package (conformalprediction.jl, and i have noticed something that i didn’t expect. i am quite new to julia and I thought that when i used import or using …

---

## [Confusion about scope in try-catch blocks](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622)

<div class="topic-metadata">

**Author:** [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Replies:** 22\
**Last updated:** [June 24, 2024, 9:07am UTC](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622 "2024-06-24T09:07:31Z")

</div>

Regarding scoping rules this used to trip me up a bit: Python: \>\>\> try: ... x = 2 ... except: ... pass ... \>\>\> x 2 Julia: julia\> try x = 2 catch end 2 julia\> x ERROR: UndefVarError:…

---

## [\`local\` just in case: good idea or code smell?](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668)

<div class="topic-metadata">

**Author:** [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Replies:** 8\
**Last updated:** [June 16, 2024, 3:55am UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668 "2024-06-16T03:55:03Z")

</div>

I often write local to ensure a variable does not accidently reuse another definition such as in the example below. Is that a good idea, or is there something more sophisticated I should be doing to test local scoping? f…

---

## [Programming languages with only global scope?](https://discourse.julialang.org/t/programming-languages-with-only-global-scope/115665)

<div class="topic-metadata">

**Author:** [@oOosys](https://discourse.julialang.org/u/oOosys)\
**Replies:** 21\
**Last updated:** [June 15, 2024, 8:36am UTC](https://discourse.julialang.org/t/programming-languages-with-only-global-scope/115665 "2024-06-15T08:36:54Z")

</div>

All of the discussion would become obsolete in case of required uniqueness of symbol names and usage of global scope variables only. The more not really needed options to choose from implemented in a programming language…

---

## [Variable Scoping in Non-Interactive Context](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603)

<div class="topic-metadata">

**Author:** [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Replies:** 1\
**Last updated:** [June 13, 2024, 2:53pm UTC](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603 "2024-06-13T14:53:56Z")

</div>

I’m trying to index a vector in a soft local variable scope, but I just can’t seem to do it. The code looks minimally something like this: in a file called testfile.jl struct TestStruct thing::Int64 end function Fo…

---

## [Discontiguous Lexical Scope for Multiply Dispatched Functions](https://discourse.julialang.org/t/discontiguous-lexical-scope-for-multiply-dispatched-functions/114972)

<div class="topic-metadata">

**Author:** [@blabatt](https://discourse.julialang.org/u/blabatt)\
**Replies:** 9\
**Last updated:** [May 31, 2024, 3:31pm UTC](https://discourse.julialang.org/t/discontiguous-lexical-scope-for-multiply-dispatched-functions/114972 "2024-05-31T15:31:51Z")

</div>

I would like to create a closure that binds free variables with respect to a discontiguous “lexical environment” that encapsulates both the disparate method implementations and collective reference to a multiply-dispatch…

---

## [Why is global scope different?](https://discourse.julialang.org/t/why-is-global-scope-different/114398)

<div class="topic-metadata">

**Author:** [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Replies:** 11\
**Last updated:** [May 17, 2024, 10:46pm UTC](https://discourse.julialang.org/t/why-is-global-scope-different/114398 "2024-05-17T22:46:30Z")

</div>

I just read through the Scope of Variables manual page and added a summary to the top of it. The main thing I still don’t understand is why global scope needs to be treated so differently from local scopes. The rational…

---

## [Parsing Julia code to separate symbols based on scoping and create a symbol table](https://discourse.julialang.org/t/parsing-julia-code-to-separate-symbols-based-on-scoping-and-create-a-symbol-table/113809)

<div class="topic-metadata">

**Author:** [@Akhil\_Akkapelli](https://discourse.julialang.org/u/Akhil_Akkapelli)\
**Replies:** 1\
**Last updated:** [May 3, 2024, 7:00pm UTC](https://discourse.julialang.org/t/parsing-julia-code-to-separate-symbols-based-on-scoping-and-create-a-symbol-table/113809 "2024-05-03T19:00:39Z")

</div>

I have a quote block in Julia containing functions, modules, and etc. Assuming that quote block is in Main module and Main module does not have these symbols with same names. I want to separate the symbols based on their…

---

## [Step!() remains strangely unfound if buried inside another method](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316)

<div class="topic-metadata">

**Author:** [@Niall](https://discourse.julialang.org/u/Niall)\
**Replies:** 5\
**Last updated:** [January 8, 2024, 9:16am UTC](https://discourse.julialang.org/t/step-remains-strangely-unfound-if-buried-inside-another-method/108316 "2024-01-08T09:16:42Z")

</div>

Hi. Can anyone please tell me why the call to step!(irg.odes) in the following code generates the error: MethodError: no method matching step!(::DynamicalSystemsBase.CoupledODEs{true, ...? Note that the call to reinit!…

---

## [Redefining print method changes variable scoping in loop](https://discourse.julialang.org/t/redefining-print-method-changes-variable-scoping-in-loop/108010)

<div class="topic-metadata">

**Author:** [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)\
**Replies:** 9\
**Last updated:** [January 5, 2024, 2:20pm UTC](https://discourse.julialang.org/t/redefining-print-method-changes-variable-scoping-in-loop/108010 "2024-01-05T14:20:18Z")

</div>

Why does the first loop work but the second does not? The MWE: Summaryusing Printf for n in 1:10 if n == 1 flag = true idx\_last = 1 else flag = false idx\_last = ifelse(flag, n, idx\_last) end # P…

---

## [Global variable issue](https://discourse.julialang.org/t/global-variable-issue/108288)

<div class="topic-metadata">

**Author:** [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Replies:** 28\
**Last updated:** [January 4, 2024, 6:24am UTC](https://discourse.julialang.org/t/global-variable-issue/108288 "2024-01-04T06:24:20Z")

</div>

It is quite strange that with the following code N = 10 M = 10 dim = binomial(N+M-1, M-1) basis=zeros(Int8, M, dim) basis\[1,1\]=N s=1 while basis\[M,s\]\<N s1=M-1 while basis\[s1,s\]==0 s1=s1-1 end …

---

## [How to get information about symbol scoping in Julia while compilation?](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643)

<div class="topic-metadata">

**Author:** [@Akhil\_Akkapelli](https://discourse.julialang.org/u/Akhil_Akkapelli)\
**Replies:** 3\
**Last updated:** [December 15, 2023, 4:01pm UTC](https://discourse.julialang.org/t/how-to-get-information-about-symbol-scoping-in-julia-while-compilation/107643 "2023-12-15T16:01:50Z")

</div>

I’m interested in understanding how Julia determines the scope of each symbol during compilation. I’d like to get detailed information about the scope of every symbol instance in the code, ideally as output from the comp…

---

## [What functions are visible in REPL?](https://discourse.julialang.org/t/what-functions-are-visible-in-repl/107612)

<div class="topic-metadata">

**Author:** [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Replies:** 3\
**Last updated:** [December 14, 2023, 3:30pm UTC](https://discourse.julialang.org/t/what-functions-are-visible-in-repl/107612 "2023-12-14T15:30:45Z")

</div>

I am using some people’s package. I tried to look up some function in the REPL with ?. But it turned that the function could not be found. The function is definitely there in the package. Is it because the function is …

---

## [Scopes: accessing a global variable hidden by a local of the same name](https://discourse.julialang.org/t/scopes-accessing-a-global-variable-hidden-by-a-local-of-the-same-name/107253)

<div class="topic-metadata">

**Author:** [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Replies:** 17\
**Last updated:** [December 9, 2023, 7:43pm UTC](https://discourse.julialang.org/t/scopes-accessing-a-global-variable-hidden-by-a-local-of-the-same-name/107253 "2023-12-09T19:43:56Z")

</div>

I’m not exactly “new to Julia” but I thought this would belong here. How do you access a global variable hidden by a local variable of the same name? Reading the documentation (see the bottom of this message), I got th…

---

## [\`outer\` keyword to automatically \`local\` a variable?](https://discourse.julialang.org/t/outer-keyword-to-automatically-local-a-variable/105964)

<div class="topic-metadata">

**Author:** [@Dan](https://discourse.julialang.org/u/Dan)\
**Replies:** 19\
**Last updated:** [November 10, 2023, 12:52am UTC](https://discourse.julialang.org/t/outer-keyword-to-automatically-local-a-variable/105964 "2023-11-10T00:52:39Z")

</div>

When using the outer keyword, as desribed in Scope of Variables · The Julia Language : julia\> function f() i = 0 for outer i = 1:3 # empty end return i e…

---

## [Confused by behavior when changing ENV\["PYTHONPATH"\] in new module](https://discourse.julialang.org/t/confused-by-behavior-when-changing-env-pythonpath-in-new-module/104408)

<div class="topic-metadata">

**Author:** [@mgalenb](https://discourse.julialang.org/u/mgalenb)\
**Replies:** 0\
**Last updated:** [September 29, 2023, 6:48pm UTC](https://discourse.julialang.org/t/confused-by-behavior-when-changing-env-pythonpath-in-new-module/104408 "2023-09-29T18:48:01Z")

</div>

I am making a module (which has a local python env installed) and I am trying to add a path to ENV\[“PYTHONPATH”\] and then import a module I am hacking in module Test ENV\["PYTHONPATH"\]=joinpath(dirname(@\_\_FILE\_\_), "pyth…

---

## [Constant changes when optimising ODE parameters](https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318)

<div class="topic-metadata">

**Author:** [@BogTak](https://discourse.julialang.org/u/BogTak)\
**Replies:** 2\
**Last updated:** [September 28, 2023, 9:48am UTC](https://discourse.julialang.org/t/constant-changes-when-optimising-ode-parameters/104318 "2023-09-28T09:48:01Z")

</div>

Hi! I’m trying to use Julia for optimizing parameters for an ODE system, but I’m running into the issue that the value C1 that should be constant is changed during the optimization. I currently have the following code (f…

---

## [Define a function in global scope inside another function](https://discourse.julialang.org/t/define-a-function-in-global-scope-inside-another-function/102597)

<div class="topic-metadata">

**Author:** [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)\
**Replies:** 6\
**Last updated:** [August 8, 2023, 7:06pm UTC](https://discourse.julialang.org/t/define-a-function-in-global-scope-inside-another-function/102597 "2023-08-08T19:06:52Z")

</div>

I want to define a function a(x) inside a function b(a), but make a in global scope, example: a(x) = 3 + x function b(a) global a(x) = a + x end This gives an error currently: ERROR: syntax: Global method definiti…

---

## [Instability with union of types with ForwardDiff and KeyedArray types, hints](https://discourse.julialang.org/t/instability-with-union-of-types-with-forwarddiff-and-keyedarray-types-hints/100772)

<div class="topic-metadata">

**Author:** [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Replies:** 1\
**Last updated:** [June 27, 2023, 8:07am UTC](https://discourse.julialang.org/t/instability-with-union-of-types-with-forwarddiff-and-keyedarray-types-hints/100772 "2023-06-27T08:07:08Z")

</div>

The goal is to calculate a number between two Vector-ish things in a type stable manner, without type assertions. This is part of a larger workflow that involves ForwardDiff.gradient, which runs, but slow, hence the inqu…

[Next page](https://discourse.julialang.org/tag/scope/198.md?match_all_tags=true&page=1&tags%5B%5D=scope)
