Julia Constructors - GeeksforGeeks

26 Mar, 2020
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 structurestruct Geeks          # Defining Fields    x    yend  # Creating a constructorGeek_constructor = Geeks(10, 20)  # Creating constructor objectsGeek_constructor.xGeek_constructor.y
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.
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 structurestruct Geeks    x    yend  # Creating a constructor with# same value for both fieldsGeek_constructor(x) = Geeks(x, x)  # Passing single argumentGeek_constructor(10)
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 typestruct Geeks      # Field    x::Real          # Creating Inner Constructor    Geeks(x) = typeof(x) != Int64 ? error("Integer value not found") : new(x)end  # Object CreationGeeks(10)Geeks(10.5)
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 typestruct Geeks{T <: Real}      # Fields    x::T    y::Tend  # Creating Outer constructorGeeks(x::T, y::T) where {T<:Real} = Geeks{T}(x, y);  # Constructor callingGeeks(10, 20)Geeks(10, 20.5)
Julia-functions
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vectors in Julia
Getting rounded value of a number in Julia - round() Method
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Storing Output on a File in Julia
Formatting of Strings in Julia
Manipulating matrices in Julia
Creating array with repeated elements in Julia - repeat() Method
Reshaping array dimensions in Julia | Array reshape() Method
while loop in Julia
Get array dimensions and size of a dimension in Julia - size() Method