Plotting an float array in julia

735 views Asked by At

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 ?

1

There are 1 answers

0
Andrej Oskin On BEST ANSWER

Well, this problem is related to the fact that you were using nothing in annotation, but correct type is Nothing (note capital N). nothing is an object, Nothing is a type of this object.

So you should use something like

function test()::Nothing
  A::Array{Float64,1} = rand(Float64, 100)
  display(plot(A))
  nothing
end

Note, that I had to add nothing as return value and explicit display 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

function test()
  A = rand(100)
  plot(A)
end

Note, that I removed all extra annotations and unnecessary Float64 in rand, since it is default value.