Hi,
I wrote a sample code which explains how inheritance can be done in Julia
Let me explain what the code is about
There is a Base Class : basic
it contains two functions A and B
Then there is number class which inherits from basic
and number has its own Function C
So number has in total three Functions A, B and C
there is integer class which inherits from number
and integer has its own Function D
So integer has in total Four Functions A, B, C and D
So what my code will be doing : I will be entering each class and printing
all the function available in the particular class
type basic
x::AbstractString
function basic()
z = new()
return z
end
function basic(p::AbstractString) # Defining Function A
z = new(p)
return z
end
end
function basic(p::Int) #Defining Function B
z = basic()
z.x = "I am Function B from basic via outer constructor"
return z
end
println("Entering basic")
println("Calling Function A...")
m = basic("I am Function A from basic via inner constructor")
println(m.x)
println("Calling Function B...")
m = basic(0)
println(m.x)
typealias number Union{basic}
function number(p::Float64) # Defining Function C
z = basic()
z.x = "I am Function C from number via number's outer constructor"
return z
end
println("number inherits basic ")
println("Entering number")
println("Calling Function A...")
m = number("I am Function A of basic via basic's inner constructor")
println(m.x)
println("Calling Function B...")
m = number(10)
println(m.x)
println("Calling Function C...")
m = number(4.5)
println(m.x)
typealias integer Union{number}
println("integer inherits number ")
println("Entering integer")
println("Calling Function A...")
m = number("I am Function A of basic via basic's inner constructor")
println(m.x)
println("Calling Function B...")
m = number(10)
println(m.x)
println("integer inherits number Calling Function C...")
m = integer(5.6)
println(m.x)
function integer(p::Float64) #Defining Function D
z = basic()
z.x = "I am Function D from integer via integer's outer constructor"
return z
end
println("Calling Function D...")
m = integer(5.6)
println(m.x)
On Tuesday, March 22, 2016 at 11:01:25 PM UTC+5:30, Stefan Karpinski wrote:
>
> Read through the julia-dev thread I linked to – this is addressed in depth
> there.
>
> On Tue, Mar 22, 2016 at 12:55 PM, Mauro <[email protected] <javascript:>>
> wrote:
>
>> > By the way isn't type Basic in my code a composite type?
>>
>> Basic is defined both as `type` and as `abstract` which will error.
>>
>> > So is there any way of inheritance among composite types ?
>>
>> No.
>>
>> > Also is there any libraries which I can refer to which uses extensive
>> > inheritance?
>> >
>> > In C++ we can easily inherit classes ? Why don't we have such ease in
>> Julia ?
>>
>> Because Julia is designed this way. Because with the ease also comes
>> pain.
>>
>> > On Tuesday, March 22, 2016 at 9:20:19 PM UTC+5:30, Stefan Karpinski
>> wrote:
>> >
>> > Have a read on this oldish thread:
>> https://groups.google.com/forum/#!topic/
>> > julia-dev/eA4VkFAD-yQ. Still applies today.
>> >
>> > On Tue, Mar 22, 2016 at 11:30 AM, kunal singh <[email protected]>
>> wrote:
>> >
>> >
>> >
>> > On Tuesday, March 22, 2016 at 8:43:50 PM UTC+5:30, Mauro wrote:
>> >
>> > > Hi Mauro ,
>> > >
>> > > Can you show me any example ?
>> > > I am a beginner in Julia. It would of great help for me.
>> >
>> > In Julia the number types are defined here:
>> > https://github.com/JuliaLang/julia/blob/
>> > fdbcdf78bf0106e609a8d83b9e896d2d11bae594/base/boot.jl#L156
>> >
>> > So there are the abstract types:
>> >
>> > abstract Number
>> > abstract Real <: Number
>> > abstract AbstractFloat <: Real
>> > abstract Integer <: Real
>> > abstract Signed <: Integer
>> > abstract Unsigned <: Integer
>> >
>> > and then there are concrete subtypes. For example a few
>> integer
>> > types:
>> >
>> > bitstype 8 Bool <: Integer
>> > bitstype 64 Int64 <: Signed
>> > bitstype 64 UInt64 <: Unsigned
>> >
>> >
>> > > On Tuesday, March 22, 2016 at 8:01:47 PM UTC+5:30, Mauro
>> wrote:
>> > >
>> > > You can only inherit from abstract types. Also, first
>> > defining
>> > >
>> > > type number
>> > > ...
>> > > end
>> > >
>> > > and then
>> > >
>> > > abstract number
>> > >
>> > > is not possible. It cannot be both abstract and
>> concrete.
>> > (Also note
>> > > that types are by convention Captialized).
>> > >
>> > > So build your hierarchy only with abstract types and
>> make
>> > concrete types
>> > > of some of the abstract ones.
>> >
>> > Can you explain this point please ?
>> > > On Tue, 2016-03-22 at 15:26, kunal singh <
>> [email protected]>
>> > wrote:
>> > > > So basically there is a type called Basic defined as
>> > follows
>> > > >
>> > > > type Basic
>> > > > ptr::Ptr{Void}
>> > > > function Basic()
>> > > > z = new(C_NULL)
>> > > > ccall((:basic_new_stack, :libsymengine), Void, (Ptr
>> > {Basic}, ), &z)
>> > > > finalizer(z, basic_free)
>> > > > return z
>> > > > end
>> > > > end
>> > > >
>> > > > Now I want to create a hierarchy: integer(not
>> Integer)<
>> > number(not
>> > > Number) <
>> > > > Basic
>> > > >
>> > > > But in Julia, we cannot inherit from concrete type So
>> what
>> > should I Do??
>> > > >
>> > > > Here's My approach
>> > > >
>> > > > abstract Basic
>> > > >
>> > > > type number <: Basic
>> > > > ptr::Ptr{Void}
>> > > > function number()
>> > > > z = new(C_NULL)
>> > > > ccall((:basic_new_stack, :libsymengine), Void, (Ptr
>> > {Basic}, ), &z)
>> > > > finalizer(z, basic_free)
>> > > > return z
>> > > > end
>> > > > end
>> > > >
>> > > > abstract number
>> > > >
>> > > > type integer <: number
>> > > > ptr::Ptr{Void}
>> > > > function integer()
>> > > > z = new(C_NULL)
>> > > > ccall((:basic_new_stack, :libsymengine), Void, (Ptr
>> > {Basic}, ), &z)
>> > > > finalizer(z, basic_free)
>> > > > return z
>> > > > end
>> > > > end
>> > > >
>> > > >
>> > > > Please tell me if I am wrong ?
>> > > > Need help from
>>
>
>