在julia中绘制浮点数组

在julia中绘制浮点数组,julia,julia-plots,Julia,Julia Plots,我有以下代码 using Plots function test()::nothing A::Array{Float64,1} = rand(Float64,100) plot(A) end 我在朱莉娅身上就是这样 julia> include("main.jl") test (generic function with 1 method) julia> test() ERROR: MethodError: First argument to `co

我有以下代码

using Plots

function test()::nothing
  A::Array{Float64,1} = rand(Float64,100)
  plot(A)
end
我在朱莉娅身上就是这样

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
function test()
  A = rand(100)
  plot(A)
end

为什么我会得到错误
要转换的第一个参数必须是一个类型,什么都没有

这个问题与您在注释中使用了
什么都没有
有关,但正确的类型是
什么都没有
(注意大写字母
N
nothing
是一个对象,
nothing
是此对象的一种类型

所以你应该用像

function test()::Nothing
  A::Array{Float64,1} = rand(Float64, 100)
  display(plot(A))
  nothing
end
注意,我必须添加
nothing
作为返回值,并显式显示
display
以显示实际绘图

但是,老实说,主要问题不是什么都没有,而是过度专业化。函数中的类型注释不会加快计算速度,您应该仅在确实需要时使用它们,例如在多次分派中

惯用代码如下所示

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
function test()
  A = rand(100)
  plot(A)
end

注意,我删除了
rand
中所有额外的注释和不必要的
Float64
,因为它是默认值。

好吧,这个问题与您在注释中使用了
nothing
有关,但正确的类型是
nothing
(注意大写
N
nothing
是一个对象,
nothing
是此对象的一种类型

所以你应该用像

function test()::Nothing
  A::Array{Float64,1} = rand(Float64, 100)
  display(plot(A))
  nothing
end
注意,我必须添加
nothing
作为返回值,并显式显示
display
以显示实际绘图

但是,老实说,主要问题不是什么都没有,而是过度专业化。函数中的类型注释不会加快计算速度,您应该仅在确实需要时使用它们,例如在多次分派中

惯用代码如下所示

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
function test()
  A = rand(100)
  plot(A)
end

请注意,我删除了
rand
中所有额外的注释和不必要的
Float64
,因为它是默认值。

好吧,您必须什么都不写。或者更好的是,什么都不做,因为除非需要,否则不需要对类型进行注释。或者更好的是,什么都不做,因为除非需要,否则不需要注释类型。