Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia UndefVarError: subtypes not defined

Tags:

julia

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)

like image 977
Damian Satterthwaite-Phillips Avatar asked Jan 26 '23 00:01

Damian Satterthwaite-Phillips


1 Answers

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]
like image 67
Przemyslaw Szufel Avatar answered Feb 05 '23 19:02

Przemyslaw Szufel