Monday 4 April 2016

Julia: User defined types (composite types)

You can define custom types using the keyword ‘type’.

type Employee
    firstName::ASCIIString
    lastName::ASCIIString
    id::Int64
end

Above snippet create new type Employee, it stores firstName, lastName and id of an employee. You can access the properties of employee using ‘.’ Notation.

emp1=Employee("Hari Krishna", "Gurram", 1)
Above statement defines employee emp1 with firstName "Hari Krishna", lastName "Gurram" and id 1.
julia> type Employee
           firstName::ASCIIString
           lastName::ASCIIString
           id::Int64
       end

julia> typeof(Employee)
DataType

julia> emp1=Employee("Hari Krishna", "Gurram", 1)
Employee("Hari Krishna","Gurram",1)

julia> emp1.firstName
"Hari Krishna"

julia> emp1.lastName
"Gurram"

julia> emp1.id
1


Custom type is similar to a class without methods in java. Custom types created with the keyword ‘type’ are mutable, i.e, you can change the values of these type.
julia> emp1
Employee("Hari Krishna","Gurram",1)

julia> emp1.firstName="Sudheer"
"Sudheer"

julia> emp1.lastName="Ganji"
"Ganji"

julia> emp1
Employee("Sudheer","Ganji",1)


Julia pass the objects to function by reference, so any changes you made to the object inside a function are visible outside also.

julia> function changeEmployee(emp; firstName=emp.firstName, lastName=emp.lastName, id=emp.id)
           emp.firstName=firstName
           emp.lastName=lastName
           emp.id=id
       end
changeEmployee (generic function with 1 method)

julia> emp1
Employee("Gopi","Ganji",1)

julia> changeEmployee(emp1, lastName="Battu", id=1234)
1234

julia> emp1
Employee("Gopi","Battu",1234)


Immutable types
Some times you want to create immutable types. You can create immutable type using the keyword immutable.

julia> immutable Student
           id::Int64
           name::ASCIIString
       end

julia> stud1=Student(1, "Krishna")
Student(1,"Krishna")

julia> stud1.id
1

julia> stud1.name
"Krishna"


You can’t change the value of immutable type. You will get an error, when you try to change the value of immutable type.

julia> stud1.id=5
ERROR: type Student is immutable

julia> stud1.name="abc"
ERROR: type Student is immutable


Benefits of immutable objects
1.   Immutable objects are inherently thread safe, so you no need to worry about thread safety.
2.   Since immutable objects state can't change, they never get into an inconsistent state.
3.   Since the properties(fields) of immutable objects can't change, you can cache the results of the operations performed on immutable objects and reuse the result.
4.   Immutable objects are best fit to use them as keys in Dictionary.
5.   Improves the performance

Type aliases
We can create a type that is similar to existing type using typealias.

julia> typealias permEmployee Employee
Employee

julia> emp1=permEmployee("Sudheer","Ganji",1)
Employee("Sudheer","Ganji",1)

julia> emp1
Employee("Sudheer","Ganji",1)

julia> typeof(emp1)
Employee


Note
Every concrete type is an instance of DataType.

julia> typeof(Employee)
DataType








Previous                                                 Next                                                 Home

No comments:

Post a Comment