# REPL's own scope

**URL:** https://discourse.julialang.org/t/repls-own-scope/21365
**Category:** Internals & Design
**Tags:** question, proposal, faq, scope
**Created:** [March 1, 2019, 8:42pm UTC](https://discourse.julialang.org/t/repls-own-scope/21365 "2019-03-01T20:42:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jeffe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffe/32/7172_2.png) [@jeffe](https://discourse.julialang.org/u/jeffe)
#### Post date: [March 1, 2019, 8:42pm UTC](https://discourse.julialang.org/t/repls-own-scope/21365/1 "2019-03-01T20:42:09Z")

</div>

Sorry for the new thread, trying not to litter @StefanKarpinski 's [new scope solution](https://discourse.julialang.org/t/new-scope-solution/16707/), which this tangents from.

Just two simple questions. I come to Julia with an understanding of scope in other languages, but…

 ![table%20cropped](https://global.discourse-cdn.com/julialang/original/3X/d/e/dee789a4a7f59d5b30bbbcea1624ba0d9607ac7a.png)

This table from the manual suggests the REPL has its own global scope. Whereas I’d thought the REPL executes in the scope of module `Main`, already in the table. Just a poorly worded table?

Yet after puzzling over this, what I’m trying now to convince myself of is why _doesn’t_ the REPL have its own scope, a local scope nested in global scope `Main`. (i.e. why doesn’t REPL initialization loosely speaking just tell the interpreter it’s in global scope `Main`, local scope `Repl` before kicking off read-eval-print loop) I don’t believe I’m speaking of `let` wrapping.

An obstacle I saw mentioned is that the function `eval` evaluates in the global scope of the containing module, so not in this hypothetical local scope `Repl`. But, seriously, then why must the REPL use `eval` instead of an alternative new function that evaluates in the local scope it’s told to? What are the technical difficulties, if someone well acquainted may summarize?

I don’t know if this requires @jeff.bezanson to explain, as an interpreting issue, but the difficulties must be great indeed, judging by the extensive effort to create alternate scope semantics in the REPL when all it really wants is to be local scope.

Thanks in advance for the education–what am I missing?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 1, 2019, 10:31pm UTC](https://discourse.julialang.org/t/repls-own-scope/21365/2 "2019-03-01T22:31:00Z")

</div>

> [@jeffe](#):
>
> This table from the manual suggests the REPL has its own global scope. Whereas I’d thought the REPL executes in the scope of module `Main`

Yes, the REPL parses the input and then evaluates it into Main. There is no REPL specific global scope.

> [@jeffe](#):
>
> why _doesn’t_ the REPL have its own scope, a local scope nested in global scope `Main` .

Local scope is more restrictive than global scope. There are some things that can only be evaluated in global scope and some things (like method definitions) only become active after global scope is encountered.

> [@jeffe](#):
>
> alternative new function that evaluates in the local scope it’s told to?

Because if evalling into local scope was allowed then you couldn’t really optimize julia code.

---

<div class="post-metadata">

### Author: ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)
#### Post date: [March 3, 2019, 5:48pm UTC](https://discourse.julialang.org/t/repls-own-scope/21365/3 "2019-03-03T17:48:52Z")

</div>

The REPL could certainly _act_ like a local scope; indeed SoftGlobalScope.jl is very close to that. The main discrepancy is this case:

```julia
julia> x = 0

julia> function foo()
           x = 1
           ...
       end

```

If this were all inside a local scope, the `x = 1` inside the function would overwrite the outer `x`. That’s not what you usually want in the REPL, since then you can’t copy and paste function definitions from elsewhere. But, if you were debugging a function with inner functions by copying it line-by-line, then it would be what you’d want. Having a REPL mode to enable that would seem reasonable to me.

But as Kristoffer said, the REPL can’t actually _be_ a local scope. The defining feature of a local scope is that the system can see every use of its variables before execution begins. That is directly at odds with a REPL, where each input has to be executed on its own.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [March 3, 2019, 7:31pm UTC](https://discourse.julialang.org/t/repls-own-scope/21365/4 "2019-03-03T19:31:45Z")

</div>

> [@jeff.bezanson](#):
>
> That’s not what you usually want in the REPL, since then you can’t copy and paste function definitions from elsewhere. But, if you were debugging a function with inner functions by copying it line-by-line, then it would be what you’d want. Having a REPL mode to enable that would seem reasonable to me.

Maybe you could have a command line option or environment variable for it. One issue with making that optional is that now you also have to specify what kind of REPL mode you are using when you share some REPL code snippets in discussion posts, since behavior would depend on REPL mode.

One solution to this would be to change the name of the REPL mode from `julia>` to `global>` maybe?
