Julia UndeVarError:未定义子类型 [英] Julia UndefVarError: subtypes not defined

查看:3
本文介绍了Julia UndeVarError:未定义子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

您需要在调用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]

这篇关于Julia UndeVarError:未定义子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆