Friday, 8 April 2016

Julia: Abstract Vs concrete types

a.   All the concrete types are final, so you can't create subtypes from a concrete type.You can create sub types for an abstract type.
b.   You can instantiate concrete types, where as you can't instantiate abstract types.
c.    Abstract types are created by using the keyword abstract, where as concrete types are created by using the keyword type.

Synatx to create abstract type
abstract «name»
abstract «name» <: «supertype»

Syntax to create concrete type
type concrete_type
    .....
    .....
end


Example
julia> type Employee
                  firstName::ASCIIString
                  lastName::ASCIIString
                  id::Int64
              end

julia> 

julia> emp1=Employee("PTR", "Nayan", 1)
Employee("PTR","Nayan",1)

julia> abstract Student

julia> stud1=Student()
ERROR: MethodError: `convert` has no method matching convert(::Type{Student})
This may have arisen from a call to the constructor Student(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{T}, ::T)
  call{T}(::Type{T}, ::Any)
 in call at essentials.jl:57

Note
When there is no super type given, then Any is the default super type.





Previous                                                 Next                                                 Home

Thursday, 7 April 2016

Julia: Create sub types

Two categories of types available in Julia.
a.   Concrete types
b.   Abstract types

Concrete types
You can create concrete types using the keyword ‘type’. You can instantiate concrete types.

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

julia> 

julia> emp1=Employee("PTR", "Nayan", 1)
Employee("PTR","Nayan",1)

When a type is applied like a function it is called a constructor. Employee("PTR", "Nayan", 1) is a constructor for the type Employee. By default, Julia creates a constructor for user defined types.

Abstract types
You can create abstract types using the keyword ‘abstract’. You can’t create an object for abstract types. Abstract types are final by default, so you can’t create subtypes to an abstract type.

julia> abstract Student

julia> stud1=Student()
ERROR: MethodError: `convert` has no method matching convert(::Type{Student})
This may have arisen from a call to the constructor Student(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{T}, ::T)
  call{T}(::Type{T}, ::Any)
 in call at essentials.jl:57


Creating subtypes
By using <: (is a subtype of) operator, you can create a sub type for an abstract type.

For example,

julia> abstract Employee

julia> type permEmployee <: Employee
           firstName::ASCIIString
           lastName::ASCIIString
           id::Int64
       end

julia> type tempEmployee <: Employee
           firstName::ASCIIString
           lastName::ASCIIString
           id::Int64
           noticePeroid::Int8
       end

julia> emp1 = permEmployee("Hari", "Krishna", 1)
permEmployee("Hari","Krishna",1)

julia> emp2 = tempEmployee("Hari", "Krishna", 1, 30)
tempEmployee("Hari","Krishna",1,30)

You can’t create a sub type for a concrete type.

julia> type SeniorEmployee <: tempEmployee
           name::ASCIIString
       end
ERROR: invalid subtyping in definition of SeniorEmployee


You can use the operator <: in expressions. It returns true when its left operand is a subtype of its right operand.

julia> tempEmployee <: Employee
true

julia> permEmployee <: Employee
true

julia> Employee <: tempEmployee
false

julia> Integer <: Number
true

julia> Integer <: AbstractFloat
false



Previous                                                 Next                                                 Home

Wednesday, 6 April 2016

Julia: Check for objects equality

By using ‘is’ function, you can check whether two objects are equal or not.

How ‘is’ function compares two mutable objects?
Compares mutable objects by address in memory, if both the objects point to same memory location then is function return true, else false.

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

julia> 

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

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

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

julia> is(emp1, emp2)
false

julia> is(emp1, emp3)
true

julia> is(emp2, emp3)
false

As you observe above snippet, even though all the three objects emp1, emp2, and emp3 has same contents, emp1 and emp2 are not equal, since they point to different memory locations.

How ‘is’ function compares two immutable objects?
Compares immutable objects by contents at the bit level.



Previous                                                 Next                                                 Home

Tuesday, 5 April 2016

Julia: Type conversion for user defined types


By using convert function, you can convert user defined types to other types.

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

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

julia> convert(::Type{Int64}, emp::Employee) = emp.id
convert (generic function with 2 methods)

julia> convert(::Type{ASCIIString}, emp::Employee) = emp.firstName
convert (generic function with 3 methods)

julia> convert(Int64, emp1)
123

julia> convert(ASCIIString, emp1)
"Hari Krishna"

convert(::Type{Int64}, emp::Employee) = emp.id
Above function convert Employee instance to integer

convert(::Type{ASCIIString}, emp::Employee) = emp.firstName
Above function convert Employee instance to ASCIIString.



Previous                                                 Next                                                 Home