Using Printf and @printf in blocks

A script like this:


using Printf
let
  @printf "%.2f" 3.14159
end

works fine.

However, if the “using Printf” is put inside the “let” block, an error occurs, as if Printf was not loaded:

let 
  using Printf
  @printf "%.2f" 3.14159
end

ERROR: LoadError: LoadError: UndefVarError: @printf not defined

The block is compiled before it is executed, and Printf needs to be loaded before the block can be compiled.

(Putting a using statement inside a let block will not restrict the scope of the exported functions anyway.)

Thank you. It confuses me a little bit, on the other hand, that this works:

let
   include("./file.jl")
end

file.jl:

using Printf
@printf "%.2f" 3.14159

I am glad it works, otherwise the includes would not be portable, but that means that
the include is not equivalent to the typing of the code in the same position.

No it isn’t and can’t be. It’s equivalent to eval.

1 Like

Excellent. Thank you.