Skip to content
Related Articles

Related Articles

Types in Julia | Set 2
  • Last Updated : 23 Apr, 2020

Prerequisite: Types in Julia | Set 1

Types in Julia are groups of data elements that are used to make program execution faster and more feasible to read. Types help in finding errors and bring clarity to the source code. Julia uses Dynamic Typed System to write variable values but it also accepts statically typed definitions to provide the advantage of indicating certain values are of specific types. By default, Julia assigns the untyped values with any type required.

Declared Types

There are three kinds of types as discussed in Set 1 of this article. These are:

  • Abstract Types
  • Primitive Types
  • Composite Types

These three kinds of types are closely related to each other. The properties of these types are almost similar:

  • They are all declared explicitly
  • They are always defined with their names
  • The supertypes of all the three kinds are declared explicitly
  • They may contain some parameters

These types are all instances of a single type called DataType because of the above-mentioned properties that they share. A DataType may be of abstract or concrete type depending upon the properties. Every concrete value is an instance of some DataType. If it’s primitive then it is of non-zero size with no specified field name and for composite datatype it must contain the field name and can be empty.
Example:
Julia-Types-11



Type Union

Assigning more than one type as an instance to a value is done with the use of Union keyword. A Type Union is a special type which includes all the instances of any argument type as objects.
Other language compilers have their own union construct but internally, which is used to decide about the type of value, but Julia allows the programmers to make their own Union set of argument types. This helps Julia to generate efficient codes with faster execution speed because, with the help of Union, it has to know check between a small number of types, declared by the programmer.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Declaring Union of types
StringorFloat = Union{AbstractString, AbstractFloat}
  
# Function with argument of Union type
function Geeks(x::StringorFloat)
    println(x)
end
  
# Function call
Geeks("Hello")
Geeks(10.5)
Geeks(10)

chevron_right


Julia-Types-12

In the above code, when the function is passed with an argument of type Int, then the compiler raises an error because the argument of the function is a Union of the types Float and String but not Integer.

Parametric Types

Julia allows to pass parameters to its types which results in generating a whole new range of types- one for each combination of parameter value.
All the declared types called DataType in general, can be parameterized. This declaration of parameterized type follows the same syntax for each of the possibilities.
There are three types of Parametric Types:

  • Parametric Composite Types
  • Parametric Abstract Types
  • Parametric Primitive Types

Parametric Composite Types

In a Parametric composite type, the parameters are written immediately after the name of the type enclosed within curly braces. The parameter here is of a general type {T}, where T can be any type.
Syntax:

struct {T}
    Field_name1::T
    Field_name2::T
end

Above given is a general syntax for parametric composite types. Here, T can be replaced with any concrete type like Int64, Float64, AbstractString, etc. Each one of these will become a usable concrete type.
Julia-Types-13

The structure name can also be used as a valid type object i.e. each of the above-created types must be a subtype of the ‘Geeks’ Type.
Julia-Types-14



All the other types which are not declared under Parameterized Types are not a subtype.
Julia-Types-15

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Type declaration for
# Parametric Composite Type
struct Geeks{T}
    x::T
    y::T
end
  
# Checking for Types
Geeks("Hello", "Geeks")
Geeks(10, 20)

chevron_right


Julia-Types-17

Parametric Abstract Types

Parametric Abstract types are declared in a similar way as the abstract types were declared. In a Parametric Abstract type, the prefix abstract type is used, the parameters are written immediately after the name of the type enclosed within curly braces. The parameter here is of a general type {T}, where T can be any type.
Syntax:

abstract type {T} end

Above given is a general syntax for parametric Abstract types. Here, T can be replaced with any concrete type like Int64, Float64, AbstractString, etc.
Julia-Types-18

As in the composite types, each parametric type will be a subtype of ‘Geeks’ Type.
Julia-Types-19

These subtypes are all different from each other and are not a subtype of one another:
Julia-Types-20

If we create another Type declaration and then define it as a subtype of the current Type declaration, then the instances of the new type will be a subtype of the existing Type:
Julia-Types-21

Parametric Primitive Types

Just like composite and abstract types, primitive types can also be declared parametrically. In a Parametric Primitive type, the prefix primitive type is used, the parameters are written immediately after the name of the type enclosed within curly braces. The parameter here is of a general type {T}, where T can be any type.
Syntax:

# For 32 bit
primitive type {T} 32 end

# For 64 bit
primitive type {T} 64 end

Above given is a general syntax for parametric Abstract types. Here, T can be replaced with any concrete type like Int64, Float64, AbstractString, etc.
Julia-Types-22

Parametric Primitive types work similarly as Composite and Abstract types.

Type Methods

Methods Description
@isdefined() Used to tests whether the specified variable s is defined in the current scope or not.
getfield() Used to extract a named field from the specified value of composite type
isefined() Used to test whether the specified global variable or object field is defined or not
oftype() Used to convert a given type of element y(say) into x(say) type of element
setfield() Used to assign a value x to a named field in value of composite type
typemax() Used to return the highest value representable by the specified numeric DataType
typemin() Used to return the lowest value representable by the specified numeric DataType
typeof() Used to return the concrete type of the specified elements
My Personal Notes arrow_drop_up
Recommended Articles
Page :