Tuesday, 12 April 2016

Julia: Union type


By using Union, you can create a custom type, which can support instances of any of its argument types, constructed using the special Union function.


julia> IntOrStringOrbool = Union{Int,AbstractString, Bool}
Union{AbstractString,Bool,Int64}

julia> 12::IntOrStringOrbool
12

julia> "hari"::IntOrStringOrbool
"hari"

julia> true::IntOrStringOrbool
true

julia> 10.23::IntOrStringOrbool
ERROR: TypeError: typeassert: expected Union{AbstractString,Bool,Int64}, got Float64

IntOrStringOrbool = Union{Int,AbstractString, Bool}
You can assign a value of type Int (or) AbstractString (or) Bool to the type IntOrStringOrbool. You will get TypeError, when you try to assign any other kind of data.



Previous                                                 Next                                                 Home

Monday, 11 April 2016

Julia: Get all the field names of composite type


‘fiednames’ function return all the field names of a composite type.
julia> type Student
           firstName::ASCIIString
           lastName::ASCIIString
           id::Int32
       end

julia> stud1 = Student("Hari Krishna","Gurram", 514)
Student("Hari Krishna","Gurram",514)

julia> fieldnames(stud1)
3-element Array{Symbol,1}:
 :firstName
 :lastName 
 :id       



Previous                                                 Next                                                 Home

Saturday, 9 April 2016

Julia: Outer Vs Inner constructor methods

In previous post, I explained about constructors in Julia. There are two ways to define constructors for a type.

a.   Outer constructor methods
b.   Inner constructor methods

a. Inner constructor methods
Inner constructor is defined inside the type declaration. It has access to the special inbuilt function new(), which is used to instantiate objects.
julia> type Employee
           id::Int64
           firstName::AbstractString
           lastName::AbstractString
           salary::Float64

           function Employee()
               this = new()

               this.id=-1
               this.firstName="no_name"
               this.lastName="no_name"
               this.salary=-1

               return this
           end

           function Employee(id::Int64, firstName::AbstractString, lastName::AbstractString, salary::Float64)
               this = new()

               this.id = id;
               this.firstName = firstName
               this.lastName = lastName
               this.salary = salary

               return this
           end
       end

Type Employee defined two inner constructors. Once a type is declared there is no way to add inner constructors to it.

b. Outer constructor methods
Outer constructors are defined outside of the type definition. Outer constructor methods depend on inner constructor methods to instantiate an object.
julia> type Dimension
           x
           y
           z

           function Dimension(x, y, z)
               this = new()
               this.x = x
               this.y = y
               this.z = z
           
               return this
           end
       end

julia> Dimension(x) = Dimension(x, x, x)
Dimension

julia> Dimension(x, y) = Dimension(x, y, 10)
Dimension

julia> dim1 = Dimension(10, 20, 30)
Dimension(10,20,30)

julia> dim2 = Dimension(20)
Dimension(20,20,20)

julia> dim3 = Dimension(1.1, 2.2)
Dimension(1.1,2.2,10)

julia> print(dim1.x, "\t",dim1.y, "\t", dim1.z)
10  20  30
julia> print(dim2.x, "\t",dim2.y, "\t", dim2.z)
20  20  20
julia> print(dim3.x, "\t",dim3.y, "\t", dim3.z)
1.1 2.2 10


‘Dimension(x)’, ‘Dimension(x,y)’ are the outer constructor methods for the type Dimension.



Previous                                                 Next                                                 Home

Julia: Constructors

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
null