I have the following code
using Plots
function test()::nothing
A::Array{Float64,1} = rand(Float64,100)
plot(A)
end
Which I run in julia like this
julia> include("main.jl")
test (generic function with 1 method)
julia> test()
ERROR: MethodError: First argument to `convert` must be a Type, got nothing
Stacktrace:
[1] test() at /path/to/main.jl:85
[2] top-level scope at REPL[2]:1
Why do I get the error First argument to convert must be a Type, got nothing
?
Well, this problem is related to the fact that you were using
nothing
in annotation, but correct type isNothing
(note capitalN
).nothing
is an object,Nothing
is a type of this object.So you should use something like
Note, that I had to add
nothing
as return value and explicitdisplay
to show actual plot.But, to be honest, main problem is not the
Nothing
, but overspecialization. Type annotations in functions do not speed up calculations, you should use them only when they are really needed, for example in multiple dispatch.Idiomatic code looks like this
Note, that I removed all extra annotations and unnecessary
Float64
inrand
, since it is default value.