【问题标题】:Julia UndefVarError: subtypes not definedJulia UndefVarError:未定义子类型
【发布时间】:2020-05-26 09:20:01
【问题描述】:

不清楚为什么我会得到 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) 上执行

【问题讨论】:

标签: julia


【解决方案1】:

您需要在调用subtypes 之前添加using InteractiveUtils。默认情况下,它在启动 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]

【讨论】:

  • 谢谢你,普热梅斯瓦夫!找不到对此的参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多