How to check whether a identifier is defined within the current scope?

Hi everyone,

Is there a way to check whether an identifier is defined in the current scope?

function f(x)
    z =1
    already_defined = #check whether z is defined here
    if already_defined
        z = do_stuff_on(z)
        z+=1
    else
       z =init_z(1)  
    end
end

Only on 0.7 this is possible:

help?> @isdefined                                                                                                                                                        
  @isdefined s -> Bool                                                                                                                                                   
                                                                                                                                                                         
  Tests whether variable s is defined in the current scope.                                                                                                              
                                                                                                                                                                         
  Examples                                                                                                                                                               
  ≡≡≡≡≡≡≡≡≡≡                                                                                                                                                             

  julia> function f()                                                                                                                                                    
             println(@isdefined x)                                                                                                                                       
             x = 3                                                                                                                                                       
             println(@isdefined x)                                                                                                                                       
         end                                                                                                                                                             
  f (generic function with 1 method)                                                                                                                                     
  
  julia> f()                                                                                                                                                             
  false                                                                                                                                                                  
  true                                                                                                                                                                   
2 Likes

Along @isdefined on 0.7 there is exist isdefined() on 0.6, thus you can use

if VERSION < v"0.7.0-DEV"
    macro isdefined(x)
        esc(isdefined(x))
    end
    export @isdefined
end

@isdefined(somevar)

I thought that only worked on the global scope.

1 Like

Yes, that only works for global variables. The @isdefined macro that works for locals requires language support and is not possible to backport to 0.6.x.

In case you still need to do this in v0.6. Here is a macro that checks if a variable is defined in a local scope: Check whether a variable is defined - #3 by mike