# Confusion about scope in try-catch blocks

**URL:** https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622
**Category:** General Usage
**Tags:** scope
**Created:** [June 11, 2024, 9:40pm UTC](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622 "2024-06-11T21:40:06Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 15, 2024, 6:26am UTC](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622/22 "2024-06-15T06:26:32Z")

</div>

> [@heliosdrm](#):
>
> the scoping behavior of try-catch blocks is more confusing than that of others because it looks like an if-else, so intuitively I would expect it to work like an if-else, but it doesn’t.

I see it the other way around, `if` is the odd one out. So I sometimes wonder how come `if` doesn’t introduce local scopes.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [June 21, 2024, 12:03pm UTC](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622/23 "2024-06-21T12:03:04Z")

</div>

Yeah, such kind of thing bit me as well.  
_And_ it works in reverse as well: let’s say there is a function which has a locally-defined function within it (maybe implicitly, via `do`-block or `@spawn` or `@threads`). Now, you have to carefully examine all variable names within the local function to avoid incorrect behavior or performance issues.

I liked the idea from [this blog bost](https://verdagon.dev/blog/on-removing-let-let-mut) of reserving `x = a` for variable initialization / declaration and using another syntax (they propose `set x = b` although I’d prefer Pascal-esque `x := b`) for assignment. That would make an easy distinction which variables in a function or closure definition are meant to be local and which to be captured.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 21, 2024, 1:33pm UTC](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622/24 "2024-06-21T13:33:26Z")

</div>

> [@Vasily\_Pisarev](#):
>
> reserving `x = a` for variable initialization / declaration and using another syntax (they propose `set x = b` although I’d prefer Pascal-esque `x := b`) for assignment

> <https://github.com/JuliaLang/julia/issues/51223>
>
> This is inspired by https://github.com/JuliaLang/julia/issues/51107 and https://…github.com/JuliaLang/julia/issues/51183 where there is ambiguity between defining and reassigning. I still dislike that when writing \`x =\` inside a function I have to worry about whether \`x\` is defined in the enclosing scope.
> 
> I like how Fortress distinguished reassignment with \`:=\` from defining with \`=\` so it couldn't happen by accident (and as a one-character "tax" on reassignment). In Scheme/Racket assignment is \`(set! x 2)\`. 
> 
> Suppose I want to always be explicit about whether I'm defining a new variable or changing the old one and have a linter disallow ambiguous cases. For defining a new variable there is \`local x\`. Could Julia also have a syntax for explicitly reassigning, like \`x := 2\` ?
> 
> Example
> 
> \`\`\`jl
> julia\> function f()
> x = 1
> function g()
> x = 2
> end
> g()
> x
> end
> f (generic function with 1 method)
> 
> julia\> f()
> 2
> \`\`\`
> 
> \--- 
> 
> Update: Pointed out on \[Discourse\](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302), a problem with the proposed syntax is that in Golang the rule is the other way around: \`:=\` is definition and \`=\` is assignment. This could potentially cause confusion for some users. However, only 3% of Julia users use Go (\[2023 Julia survey\](https://julialang.org/assets/2023-julia-user-developer-survey.pdf)) and, sociologically speaking, afaict Go's market doesn't have too much overlap with Julia's. Moreover, SQL and Mathematica, which share far more users with Julia, already use \`=\` for different meanings without problem. Definition syntax is so ubiquitous everybody's gonna understand very quickly what the rule is. So I suspect it’s relatively unlikely to cause excessive confusion in practice. Also, having the slightly shorter \`=\` syntax be for definition rather than reassignment is a nice mild encouragement in the right direction.
> 
> Stefan suggested an opt-in to requiring \`:=\` for reassignment, or using \`\<-\` for reassignment.

> [@On using \`=\` vs \`:=\` for assignment](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302):
>
> news to me

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [June 24, 2024, 9:07am UTC](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622/25 "2024-06-24T09:07:31Z")

</div>

I haven’t thought of a linter support. Wow, it looks that if it would work, then an additional syntax for re-assignment is useful even with Julia 1.x.

[Previous page](https://discourse.julialang.org/t/confusion-about-scope-in-try-catch-blocks/115622.md?page=1)
