I have originally opened an issue in RecipesBase.jl, but I think this is just me not understanding the machinery of plot recipes. Why the code below fails when I try to pass in a list of keywords with the ... syntax?
using RecipesBase
#userplot FooPlot
#recipe function f(fp::FooPlot; a=1, b=2, kwargs...)
# SKIP
end
The error I am getting:
BoundsError: attempt to access 1-element Array{Any,1} at index [2]
I appreciate any help.
Related
Is it possible to type function kwargs in Julia?
The following works for standard Varargs.
function int_args(args::Integer...)
args
end
int_args(1, 2, 3)
# (1, 2, 3)
int_args(1, 2, 3.0)
# ERROR: MethodError: `int_args` has no method matching int_args(::Int64, ::Int64, ::Float64)
However, when applying this same syntax to kwargs, all function calls seem to error.
function int_kwargs(; kwargs::Integer...)
kwargs
end
int_kwargs(x=1, y=2)
# ERROR: MethodError: `__int_kwargs#0__` has no method matching __int_kwargs#0__(::Array{Any,1})
Normal keyword arguments can have types, as in function f(x; a::Int=0), but this doesn't work for "rest" keyword arguments. Also note that since we currently don't dispatch on keyword arguments, the a::Int in this case is a type assertion and not a dispatch specification.
It looks like this case is not handled well, and needs a better error message at least. I'd encourage you to file an issue at https://github.com/JuliaLang/julia/issues.
I'm not sure what the syntax x::T... should mean for keyword arguments. In the case of varargs, it's clear that each element of x should have type T, but for rest keyword arguments each element is actually a symbol-value pair. Of course we could give it the meaning you describe (all values have type T), but this doesn't seem to come up very often. Keyword arguments tend to be quite heterogeneous, unlike varargs which are more like lists or arrays.
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 have a script that defines a function, and later intended to call the function but forgot to add the parentheses, like this:
function myfunc()
println("inside myfunc")
end
myfunc # This line is silently ignored. The function isn't invoked and there's no error message.
After a while I did figure out that I was missing the parentheses, but since Julia didn't give me an error, I'm wondering what that line is actually doing? I'm assuming that it must be doing something with the myfunc statement, but I don't know Julia well enough to understand what is happening.
I tried --depwarn=yes but don't see a julia command line switch to increase the warning level or verbosity. Please let me know if one exists.
For background context, the reason this came up is that I'm trying to translate a Bash script to Julia, and there are numerous locations where an argument-less function is defined and then invoked, and in Bash you don't need parentheses after the function name to invoke it.
The script is being run from command line (julia stub.jl) and I'm using Julia 1.0.3 on macOS.
It doesn't silently ignore the function. Calling myfunc in an interactive session will show you what happens: the call returns the function object to the console, and thus call's the show method for Function, showing how many methods are currently defined for that function in your workspace.
julia> function myfunc()
println("inside myfunc")
end
myfunc (generic function with 1 method)
julia> myfunc
myfunc (generic function with 1 method)
Since you're calling this in a script, show is never called, and thus you don't see any result. But it doesn't error, because the syntax is valid.
Thanks to DNF for the helpful comment on it being in a script.
It does nothing.
As in c, an expression has a value: in c the expression _ a=1+1; _ has the value _ 2 _ In c, this just fell out of the parser: they wanted to be able to evaluate expressions like _ a==b _
In Julia, it's the result of designing a language where the code you write is handled as a data object of the language. In Julia, the expression "a=1+1" has the value "a=1+1".
In c, the fact that
a=1+1;
is an acceptable line of code means that, accidentally,
a;
is also an acceptable line of code. The same is true in Julia: the compiler expects to see a data value there: any data value you put may be acceptable: even for example the data value that represents the calculated value returned by a function:
myfunc()
or the value that represents the function object itself:
myfunc
As in c, the fact that data values are everywhere in your code just indicates that the syntax allows data values everywhere in your code and that the compiler does nothing with data values that are everywhere in your code.
isdefined(:x) will tell you if a variable x is defined in your current workspace.
If I want to check a variable is defined in a module (not one that's exported), how can I do that? I tried all of the following:
julia> module Test
x = 1
end
Test
julia> x
ERROR: UndefVarError: x not defined
julia> isdefined(:x)
false
julia> Test.x
1
julia> isdefined(:Test.x)
ERROR: type Symbol has no field x
julia> isdefined(:Test.:x)
ERROR: TypeError: getfield: expected Symbol, got QuoteNode
julia> isdefined(Test.:x)
ERROR: TypeError: getfield: expected Symbol, got QuoteNode
In the module Test above, I want to check if x is defined or not.
isdefined has an optional parameter for doing this. Try:
isdefined(Test, :x)
More information available through the usual channels: ?isdefined on the REPL and in the book: http://docs.julialang.org/en/release-0.4/stdlib/base/#Base.isdefined (link may be for older version, so the currently dominant search engine will help).
I think you need
:x in names(Test)
In Julia 1.1.0, isdefined's first parameter is not optional, instead there is a macro #isdefined(x) or #isdefined x that tests if x is defined. Calling it inside Test, checks if x is defined when in Test (inherited or not).
See documentation.
The following code in Julia:
function foo(a::Vector{AbstractString})
end
foo(["a"])
gives the following error:
ERROR: MethodError: no method matching foo(::Array{String,1})
Closest candidates are:
foo(::Array{AbstractString,1}) at REPL[77]:2
Even though the following code runs, as expected:
function foo(a::Vector{String})
end
foo(["a"])
And further, AbstractString generally matches String as in:
function foo(::AbstractString)
end
foo("a")
How can I call a function with a Vector{AbstractString} parameter if I have String elements?
You need to write the function signature like this:
function foo{S<:AbstractString}(a::Vector{S})
# do stuff
end
On Julia 0.6 and newer, it's also possible to write instead
function foo(a::Vector{<:AbstractString})
# do stuff
end
This is a consequence of parametric type invariance in Julia. See the chapter on types in the manual for more details.