Saturday, 9 April 2016

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

No comments:

Post a Comment