Top level vs global scope

The Julia docs contain the terms “top level” and “global scope”. Is the relation between the two this:

"top level" === "global scope"

or this:

"top level" ≈ "global scope"

or something else entirely?

1 Like

An example of where the former is used might be helpful. I understand what “global scope” means, and I think it has specific, technical definition. “Top level” seems like it could mean a lot of things, but I don’t know.

When used to describe scope the two are the same.

“top level” is sometimes more specific though, to refer to the, well, top level syntax structure (i.e. not within anything other than the enclosing module). Certain syntax are only allowed at top level, e.g.,

julia> begin
       module A
       end
       end
ERROR: syntax: "module" expression not at top level
3 Likes

The term top-level appears 3 times in the chapter on scope of variables in the manual. As @yuyichao mentioned, it appears that 2 out of 3 of those usages are basically synonymous with global scope. However, this note is a little confusing:

In this and all following examples it is assumed that their top-level is a global scope with a clean workspace, for instance a newly started REPL.

To me that implies that there is some distinction between top-level and global scope, but it’s not clear to me from that sentence what the distinction is.

And of course as @yuyichao pointed out, the other place where the term top-level sometimes shows up is in error messages or stacktraces.

@yuyichao Do you mean that certain syntax are only allowed at the uppermost node in the syntax tree?

This top-level is roughly the same as what said above, it basically means tracing up the enclosing syntax until the module.

No. Nested module is allowed, for example. They just cannot be wrapped in any other syntax within the module.

3 Likes