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)
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]
Related
I am trying to define a struct in Julia as follows:
julia> struct
myName::String
end
ERROR: syntax: invalid type signature around REPL[2]:1
Stacktrace:
[1] top-level scope
# REPL[2]:1
however I am getting the error shown above. Any suggestions to resolve this?
Silly me, the issue is the struct requires a name which I did not specific. I needed to do something like this:
julia> struct person
myName::String
end
which resolves the issue.
I've inherited some old code, which was written for a much earlier version of Julia (~v0.6). I'm running Julia 1.5.4 and I've already adjusted a lot of the existing code, without much trouble.
But when I run:
import Models
type GLMA <: Models.Fitting.AbstractFittingAlgorithm
end
I get
ERROR: syntax: extra token "GLMA" after end of expression
Stacktrace:
[1] top-level scope at none:1
Why is this happening?
(a) Is there an issue with the type part?
Additionally, (b) Why is Models.Fitting missing when I do this:
Models.Fitting
ERROR: UndefVarError: Fitting not defined
Stacktrace:
[1] getproperty(::Module, ::Symbol) at .\Base.jl:26
[2] top-level scope at REPL[4]:1
I think you might have to declare it as an abstract type rather than a "bare" type. For example:
import Models
abstract type GLMA <: Models.Fitting.AbstractFittingAlgorithm; end
I don't see any bare type references, just abstract and primitive:
Types
Abstract Types
So with the following I was able to reproduce your error message:
julia> abstract type X end
julia> type Y <: X end
ERROR: syntax: extra token "Y" after end of expression
Stacktrace:
[1] top-level scope at none:1
A remote possibility is that the old code brought Fitting into scope with import or using and the keyword 'as' and if that renaming is not currently done, then you've lost that link. More here:
https://docs.julialang.org/en/v1/manual/modules/#Renaming-with-as
Julia is dynamically typed and certain errors only occur during execution.
For instance:
julia> function foo()
a
5
end
foo (generic function with 1 method)
julia> foo()
ERROR: UndefVarError: a not defined
Stacktrace:
[1] foo() at ./REPL[1]:2
[2] top-level scope at none:0
julia>
Same behaviour using parse in combination with eval:
julia> eval(Meta.parse("function foo()
a
5
end"))
foo (generic function with 1 method)
However, when executing this an error is thrown:
julia> foo()
ERROR: UndefVarError: a not defined
Stacktrace:
[1] foo() at ./none:2
[2] top-level scope at none:0
Does it exist any standard facilities to check against these kinds of errors? Or does it exist any suitable packages for this task?
The VS Code Julia extension has an integrated linter that can detect usage of undefined variables, as in your example.
There is also this linter, Lint.jl, though I am not sure if it's up-to-date for Julia v1.x.
I made a module with an if condition on the number of cores.
module mymodule
import Pkg
import PyCall
using Distributed
if nworkers() > 1
#everywhere using Pkg
#everywhere Pkg.activate(".")
#everywhere Pkg.instantiate()
#everywhere using PyCall
#everywhere #pyimport scipy.signal as ss
function parallel()
....
end
else
using Pkg
Pkg.activate(".")
Pkg.instantiate()
using PyCall
#pyimport scipy.signal as ss
function serial()
....
end
end
end #mymodule
Code throws the following error on execution
ERROR: LoadError: LoadError: UndefVarError: #pyimport not defined
Stacktrace:
[1] top-level scope
[2] include at ./boot.jl:326 [inlined]
[3] include_relative(::Module, ::String) at ./loading.jl:1038
[4] include(::Module, ::String) at ./sysimg.jl:29
[5] include(::String) at ./client.jl:403
[6] top-level scope at none:0
in expression starting at /storage/work/s/mymodule.jl:81
in expression starting at /storage/work/s/mymodule.jl:30
where line 81 is the line in else condition corresponding to #pyimport scipy.signal as ss and line 30 corresponds to if nworkers() > 1.
Before this problem, the code had an issue with the line #everywhere #pyimport scipy.signal as ss but that disappeared by using import PyCall; bizarrely though, it didn't solve the former problem.
Has anyone experienced a similar issue or aware of such issues?
You need to use pyimport function instead. Macro definition (comes from your using) and usage of that macro in the same block does not work due to the parsing/evaluation order.
Simply change the code
#pyimport scipy.signal as ss
to
ss = pyimport("scipy.signal")
You can also divide the blocks into two, first for the definitions and the second for usage. However, I would not do that as #pyimport macro is already deprecated.
I am learning Julia and for that purpose I am building a simulation of a scheduler for some non specified task. For this I want to create an abstract type, which represents the idea of state. A task could be in any state Accepted, Unfulfilled, Running, Finished:
abstract RequestState
Then I add some concrete states, which have RequestState as superstate:
immutable StateAccepted <: RequestState
name :: String
end
And because each state is immutable anyway and it doesn't have an interesting structure, all instances are the same, so I instantiate an instance of the above type to use:
const Accepted = StateAccepted("Accepted")
I generate this with a macro, perhaps it is relevant.
Now comes the problem. I want to write a function, which checks valid transitions. I only have a skeleton at this point:
function checkTransition{T <: RequestState, Q <: RequestState}(t :: T, q :: Q) :: Bool
return true
end
I assumed I should read the expression:
T <: RequestState
As something like forall T which is a subtype of RequestState, but I think this is wrong.
I would expect that this code compiles. The problem is that I don't understand the error and also can't find anything in the documentation. Note that this could be, because I am unfamiliar with the language. The relevant error is:
Error During Test
Expression evaluated to non-Boolean
Expression: checkTransition(Accepted,Running)
Value: ResourceScheduler.StateRunning("Running")
ERROR: LoadError: There was an error during testing
in record(::Base.Test.FallbackTestSet, ::Base.Test.Error) at ./test.jl:397
in do_test(::Base.Test.Returned, ::Expr) at ./test.jl:281
in include_from_node1(::String) at ./loading.jl:488
in process_options(::Base.JLOptions) at ./client.jl:262
in _start() at ./client.jl:318
while loading /home/eklerks/.julia/v0.5/ResourceScheduler/test/runtests.jl
How would I make a generic function accepting only arguments of subtypes of RequestState?
EDIT:
As requested the offending test:
#test checkTransition(Accepted, Running)
But this didn't work out, because I forgot to update and install my package after I changed it. Before that change it read:
function checkTransition{T <: RequestState, Q <: RequestState}(t :: T, q :: Q) :: Q
return q
end
Which was the source of the error. Type Q is indeed not a Boolean. I was experimenting with the type system at that moment.
While your code should work, typically one writes
checkTransition(t::RequestState, q::RequestState) = true
instead of using the unnecessary T <: RequestState parameterization. Note that f{T <: X}(::T) should not be read as "for all T subtype of X", but rather "there exists T subtype of X", as this is an existential type and not a universal type.
The error you're getting is due to checkTransition(Accepted,Running) returning a non-boolean, which must be due to a mistake unrelated to the code that you've posted.