TOPLEVEL Expressions

What is a top level expression?
And what is the difference between expressions whose head is :toplevel and :top?


1 Like

Anyone willing to answer?

I am not the most knowledgeable about this, but perhaps this helps: in Julia, certain forms are only allowed at the top level, ie directly evaluated in the context of a module (as opposed to being inside a function, etc).

When a toplevel form expands to multiple expressions which are themselves toplevel, it is wrapped in a :toplevel to preserve this:

julia> expand(:(import Foo: x, y))
:($(Expr(:toplevel, :(import Foo.x), :(import Foo.y))))

You can find these in the parser code, and in the documentation.

1 Like

Thanks for the answer of first part of my question. Anyone willing to answer what :top is used for?

:top is only generated by evalfile. See ast.c, loading.jl.

In my package Reduce.jl it is used like this:

bas = [:foo,:bar]
Expr(:toplevel,[:(import Base: $i) for i ∈ bas]...) |> eval

so is :top symbol is used for describing a top type like Any in Scala, or Object in java?

Not at all. It is an expression in the AST. Note that Julia has Any.

To understand the type system, please read the manual first. If you want help with AST’s, describing what you are trying to do would be helpful.