Julia UndefVarError: subtypes not defined

Unclear why I get ERROR: LoadError: UndefVarError: subtypes not defined when executing a .jl file, but not when executed from the REPL.

E.g.

abstract type Asset end

abstract type Property <: Asset end
abstract type Investment <: Asset end
abstract type Cash <: Asset end
println(subtypes(Asset))

> 3-element Array{Any,1}:
 Cash
 Investment
 Property

...but put the very same code in test.jl,

julia test.jl

> ERROR: LoadError: UndefVarError: subtypes not defined
Stacktrace:
 [1] top-level scope at /.../test.jl:6
 [2] include(::Module, ::String) at ./Base.jl:377
 [3] exec_options(::Base.JLOptions) at ./client.jl:288
 [4] _start() at ./client.jl:484
in expression starting at /.../test.jl:6

Julia version 1.4.1, executing on OSX Catalina (10.15.4)

Upvotes: 7

Views: 1035

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42194

You need to add using InteractiveUtils before calling subtypes. By default this is already loaded when starting the Julia REPL.

Hence your file should look like this:

shell> more t.jl

using InteractiveUtils
abstract type Asset end

abstract type Property <: Asset end
abstract type Investment <: Asset end
abstract type Cash <: Asset end
println(subtypes(Asset))


shell> julia t.jl
Any[Cash, Investment, Property]

Upvotes: 12

Related Questions