Julia UndefVarError : subtypes not defined

标签 julia

不清楚为什么我得到ERROR: LoadError: UndefVarError: subtypes not defined执行 .jl 文件时,而不是从 REPL 执行时。

例如。

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

...但在 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 版本 1.4.1,在 OSX Catalina (10.15.4) 上执行

最佳答案

您需要添加 using InteractiveUtils调用前 subtypes .默认情况下,它在启动 Julia REPL 时已经加载。

因此,您的文件应如下所示:

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]

关于 Julia UndefVarError : subtypes not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62012171/

相关文章:

julia - Flux.jl 有张量对象吗?

http - 使用 HTTP.jl 发送大文件

julia - 如何在 Atom 中运行 Julia 脚本

julia - 如何创建一个接受任何可迭代字符串集合的方法?

julia - 在 Julia 中对整数数组求和的有效方法

python - 非线性方程组 Julia

gtk - 将Gadfly绘图直接绘制到Gtk Canvas 上

debugging - 为什么 Juno 调试器尝试在某个随机目录中搜索文件?

Julia:具有抽象参数的数组会导致错误,但具有抽象类型的变量不会

julia - in() 函数或 in 运算符的广播版本?