在Julia中定义常量全局变量

在Julia中定义常量全局变量,julia,Julia,在Julia中,将变量定义为const和将变量定义为const global有什么区别?考虑以下示例,如果我将const global更改为const,有什么区别 #set number of cores number_cores=7; addprocs(number_cores) #include necessary functions @everywhere include("$(pwd())\\lib.jl"); const global n = 2; # number of obse

在Julia中,将变量定义为
const
和将变量定义为
const global
有什么区别?考虑以下示例,如果我将
const global
更改为
const
,有什么区别

#set number of cores
number_cores=7;
addprocs(number_cores)

#include necessary functions
@everywhere include("$(pwd())\\lib.jl");

const global n = 2; # number of observables
const global k = nVAR + 2*n-1; # number of states
const global m = k*q;

pmap(a->parallel_un(a,n,k,m),1:7)
有两种情况:

  • 如果您在全局范围内,则
    const
    const-global
    是相同的
  • 如果您不在全局范围内,那么在Julia 0.7中不推荐使用
    const
    ,使用
    const global
    是可以接受的,并定义了一个全局常量
  • 以下是一个示例会话:

                   _
       _       _ _(_)_     |  A fresh approach to technical computing
      (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
       _ _   _| |_  __ _   |  Type "?help" for help.
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 0.7.0-DEV.3404 (2018-01-14 21:52 UTC)
     _/ |\__'_|_|_|\__'_|  |  Commit d569a2923c* (30 days old master)
    |__/                   |  x86_64-w64-mingw32
    
    julia> f() = (const global x = 1)
    f (generic function with 1 method)
    
    julia> f()
    1
    
    julia> x = "a"
    ERROR: invalid redefinition of constant x
    
    julia> g() = (const global y = 1)
    g (generic function with 1 method)
    
    julia> y = 1
    1
    
    julia> g()
    ERROR: cannot declare y constant; it already has a value
    Stacktrace:
     [1] g() at .\REPL[4]:1
     [2] top-level scope
    

    作为旁白-为什么在
    pmap
    之前使用
    @sync@async
    ?我认为它应该没有效果?你是对的,在这种情况下,
    @sync@async
    是不必要的。