TOPLEVEL Expressions


#1

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

Thanks


#2

Anyone willing to answer?


#3

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.


#4

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


#5

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


#6

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

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

#7

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


#8

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.