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
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.
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]
Julia> abstract Shape
ERROR: syntax: extra token "Shape" after end of expression
Stacktrace:
[1] top-level scope at REPL[64]:0
Just wanted to create an abstract but it says following even when I use help?>
Is there any alternate to this?
help?> abstract
search: AbstractSet abstract type AbstractChar AbstractDict AbstractFloat
Couldn't find abstract
Perhaps you meant struct or AbstractSet
No documentation found.
Binding abstract does not exist.
The correct syntax is abstract type Shape end.
You can find the docs in the REPL help mode with the full keyword:
help?> abstract type
There is online documentation on the keyword and more general documentation on types.
I am using a julia v0.6 code. But my julia version is v1.1. I don't know how to modify this piece of code
roadway_HOLO = open(io->read_dxf(io, Roadway, dist_threshold_lane_connect=2.0), joinpath(#__DIR__, "../data/ngsim_HOLO.dxf"), "r")
convert_curves_feet_to_meters!(roadway_HOLO)
open(io->write(io, roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
The error occurs at the last line of the code. Please help.
ERROR: LoadError: LoadError: MethodError: no method matching write(::IOStream, ::AutomotiveDrivingModels.Roadway)
Closest candidates are:
write(::IO, ::Any) at io.jl:498
write(::IO, ::Any, ::Any...) at io.jl:500
write(::IOStream, ::UInt8) at iostream.jl:378
...
Stacktrace:
I believe you are using this AutomotiveDrivingModels.jl. It seems to be that you need to pass MIME("text/plain") to the call to write in order to write your Roadway object to a text file.
open(io->write(io, MIME("text/plain"), roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
# or
open(io->write(io, MIME"text/plain"(), roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
Note that you can also use do syntax with methods whose first argument is a Function like open. The call above is equivalent to the following call below.
open(joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w") do io
write(io, MIME("text/plain"), roadway_HOLO)
end
As a side note, methods(write) should normally show the write method(s) for Roadway type and also ?write should show the docstring for this write method. If you happen to see a similar error in the future, you might want to try these to find the correct method signature.
Use Serialization to store Julia objects in a file:
Consider some custom data structure and some object.
struct Some
x::String
y::Int
end
s = Some("test 123",12345)
The above structure can be serialized with the following command:
using Serialization
open("file.bin","w") do f
serialize(f,s)
end
Now let us test deserialization:
julia> open("file.bin") do f; println(deserialize(f)==s); end
true
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.