Function depending on the global variable inside module

Yes, one or other option. The use of an assertion allow you to throw a customized error message.

(personally I would like to have an option to throw an error without launching the complete stack trace, meaning, I would like to have something that behaves like this:

julia> function f(x)
         if typeof(x) != Int
           println("x must be an Int")
           return nothing
         end
         x
       end
f (generic function with 1 method)

julia> f(1.0)
x must be an Int

Instead of:

julia> function f(x)
         @assert typeof(x) == Int "x must be an Int"
         x
       end
f (generic function with 1 method)

julia> f(1.0)
ERROR: AssertionError: x must be an Int
Stacktrace:
 [1] f(x::Float64)
   @ Main ./REPL[6]:2
 [2] top-level scope
   @ REPL[7]:1

In this case the stack trace is not large, but in some cases it can be overwhelming for a user.

1 Like