Open In App

Julia Constructors

Improve
Improve
Like Article
Like
Save
Share
Report

Constructors in Julia are functions that are used to create objects. They are specifically used to create instances of objects of Composite Types. A composite type is a collection of named fields, which can also be treated as single values. Julia allows creating type objects as constructors when they are assigned to a set of argument values passed in the form of a function. A constructor works on the basic data structure known as structure or Types in Julia. A type is created with the use of a predefined keyword struct.

struct Structure_name
    Field1
    Field2
end

constructor1 = Structure_name(value1, value2)

constructor1.Field1
constructor1.Field2

Example:




# Creating a structure
struct Geeks
      
    # Defining Fields
    x
    y
end
  
# Creating a constructor
Geek_constructor = Geeks(10, 20)
  
# Creating constructor objects
Geek_constructor.x
Geek_constructor.y


Julia-Constructors-01

In the above code, when the constructor is created with a function by passing argument values, those values are automatically instantiated to the Fields of the structure individually at the time of object creation.

Types of Constructors

Julia allows creating two types of constructors based on their behavior. These are:

  • Outer constructor
  • Inner constructor

Outer Constructor –
Outer constructor methods are defined outside of the block of type declaration. An outer constructor is just like any other function in Julia, its functionality depends on all the methods that are defined inside of it. A new functionality can be added to a constructor by simply defining a new method.
Example:




# Creating a structure
struct Geeks
    x
    y
end
  
# Creating a constructor with
# same value for both fields
Geek_constructor(x) = Geeks(x, x)
  
# Passing single argument
Geek_constructor(10)


Julia-Constructors-02
As in the above code, the constructor is created with one value and that single value is assigned to both the fields.
Hence, the constructor methods created like normal methods are called outer constructors. An outer constructor method can only create new instances by calling other constructors.

Inner Constructor –
Unlike the outer constructor method in which the constructor can be defined anywhere in the source code just like any normal function, inner constructors can only be defined inside of the block of the type declaration. It is just like the outer constructor method except that it can also access a special pre-defined function termed as new. Defining a constructor within the type block will restrict the object creation to be bound as per the condition defined during the constructor creation.

Example:




# Defining type
struct Geeks
  
    # Field
    x::Real
      
    # Creating Inner Constructor
    Geeks(x) = typeof(x) != Int64 ? error("Integer value not found") : new(x)
end
  
# Object Creation
Geeks(10)
Geeks(10.5)


Julia-Constructors-03

Parameteric Constructor

A parametric constructor type is one in which the datatype of the Structure is pre-defined at the time of Type declaration. The fields of this structure are implied with a datatype at the time of Type declaration. In a parametric constructor, the constructor calls in which the parameter type is explicitly defined are converted to the implied field types. Hence, the type implied to the parameters at the time of function call must agree otherwise an error will be raised, but any pair of values with matching types can be passed to the constructor.

Example:




# Defining type
struct Geeks{T <: Real}
  
    # Fields
    x::T
    y::T
end
  
# Creating Outer constructor
Geeks(x::T, y::T) where {T<:Real} = Geeks{T}(x, y);
  
# Constructor calling
Geeks(10, 20)
Geeks(10, 20.5)


Julia-Constructors-04



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