Open In App

Check if the objects or variables are defined in Julia – isdefined() and @isdefined() Methods

Improve
Improve
Like Article
Like
Save
Share
Report

The isdefined() is an inbuilt function in julia which is used to test whether the specified global variable or object field is defined or not. The passed parameters can be a module and a symbol or a composite object and field name (as a symbol) or index.

Syntax:
isdefined(m::Module, s::Symbol)
or
isdefined(object, s::Symbol)
or
isdefined(object, index::Int)

Parameters:

  • m::Module: Specified module.
  • s::Symbol: Specified symbol.
  • object: Specified composite object.
  • index::Int: Specified index.

Returns: It returns true for the defined specified global variable or object field else returns false.

Example:




# Julia program to illustrate 
# the use of isdefined() method
   
# Getting true for the defined 
# specified global variable or 
# object field else returns false.
println(isdefined(Base, :sum))
println(isdefined(Base, :num))
println(isdefined(Base, :NonExistentMethod))
  
# Initialising a composite type value with 
# Floor division operator
a = 2//3;
println(isdefined(a, 1))
println(isdefined(a, 2))
println(isdefined(a, :num))
println(isdefined(a, :numerator))


Output:

true
true
false
true
true
true
false

@isdefined()

The @isdefined() is an inbuilt function in julia which is used to tests whether the specified variable s is defined in the current scope or not.

Syntax:
@isdefined s

Parameters:

  • s: Specified variable.

Returns: It returns true if the specified variable s is defined in the current scope else returns false.

Example:




# Julia program to illustrate 
# the use of @isdefined() method
   
# Getting true if the specified variable
# s is defined in the current scope
# else returns false.
function f()
    println(@isdefined x)
    x = 10
    println(@isdefined x)
    end
f (generic function with 1 method)
println(f())


Output:



Last Updated : 01 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads