Why does julia not recognize the type of an array that is passed as a function argument, listing it as Any instead?

血红的双手。 提交于 2020-01-04 06:38:29

问题


I am defining a function in julia that accepts a vector (specifically Vector{Complex128}). When I look at the output of @code_warntype I see that the variable type is listed as Any. This can potentially have speed implications, as I understand. Here is a simple version of the code, for example:

function abc(h::Vector{Complex128})
   a=1+2
end

The output from @code_warntype is

julia> @code_warntype abc(zeros(Complex128,2))
Variables:
  #self#::#abc
  h::Any
  a::Int64

Body:
  begin 
    SSAValue(0) = (Base.add_int)(1, 2)::Int64
    return SSAValue(0)
  end::Int64

The type of the variable h is listed as Any. I am new to julia and don't really know if I am missing something here. This behaviour doesn't seem specific to Vector{Complex128}, I get the same behaviour with Vector{Float64} as well. Am I annotating the variable type incorrectly here, or is this how it is supposed to work? I'm using julia v0.6.0, if that matters.


回答1:


This is because the compiler has optimized it away so it doesn't even exist. The way it's printed has changed in v0.6.1 to be more clear:

julia> function abc(h::Vector{Complex128})
          a=1+2
       end
abc (generic function with 1 method)

julia> @code_warntype abc(zeros(Complex128,2))
Variables:
  #self# <optimized out>
  h <optimized out>
  a <optimized out>

Body:
  begin
      SSAValue(0) = (Base.add_int)(1, 2)::Int64
      return SSAValue(0)
  end::Int64


来源:https://stackoverflow.com/questions/47201832/why-does-julia-not-recognize-the-type-of-an-array-that-is-passed-as-a-function-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!