Pkg.installed(“anInvalidPackage”),什么都不返回或抛出错误?

时间:2016-11-27 07:27:38

标签: julia

参考本段:

  

已安装(pkg)→无效| VERSIONNUMBER

If pkg is installed, return the installed version number, otherwise return nothing.

来自JuliaDoc

Pkg.installed("anInvalidPackage")函数应返回nothing,但会抛出错误:

julia> VERSION
v"0.5.0"
julia> Pkg.installed("anInvalidPackage")
ERROR: anInvalidPackage is not a package (not registered or installed)
 in installed(::String) at .\pkg\entry.jl:123
 in (::Base.Pkg.Dir.##2#3{Array{Any,1},Base.Pkg.Entry.#installed,Tuple{String}})
() at .\pkg\dir.jl:31
 in cd(::Base.Pkg.Dir.##2#3{Array{Any,1},Base.Pkg.Entry.#installed,Tuple{String}
}, ::String) at .\file.jl:48
 in #cd#1 at .\pkg\dir.jl:31
 in installed(::String) at .\pkg\pkg.jl:129

编辑issue

1 个答案:

答案 0 :(得分:2)

如果我们在基础上查看Pkg模块的源代码,它应该使用一些线索:

<div class="wrapper">
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <br /> <!-- NOTE: Line break here for the next row! -->
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>
  <div class="services-box">
    <div class="service">
      <h5>Lorem ipsum</h5>
      <p>Lorem ipsum dolor sit amet
        </p>
          <a href="#">More</a>
    </div>
  </div>


</div>

从最后两行我们可以看出,如果包未注册或已安装,Julia将抛出错误。但是,如果包已注册但未安装,则不会引发任何错误并返回function installed(pkg::AbstractString) avail = Read.available(pkg) if Read.isinstalled(pkg) res = typemin(VersionNumber) ### some libgit2 stuff ### end isempty(avail) && throw(PkgError("$pkg is not a package (not registered or installed)")) return nothing # registered but not installed end

所以

nothing

可是:

var = Pkg.installed("AMD") # returns nothing for me as I don't have AMD installed
                           # but it is a registered package

我同意文档应该更清楚......