DEV Community

Cover image for Learn Julia (9): still about Types in Julia
Z. QIU
Z. QIU

Posted on

1

Learn Julia (9): still about Types in Julia

Reminder: this post is an extension learning note of my previous post Learn Julia (7): apropos Types.

Julia is not an OOP language, meaning that there's no Class concept in it. However, there is a quite sophisticated Datatype hierarchy in Julia, making it possible to do some Class-like stuff. This is the subject of today's learning note.

Today I get familiar with the following concept about Datatype of Julia:

  1. subtyping rules
  2. abstract type
  3. primitive Types
  4. composite Types and mutable composite Types
  5. parametric Abstract Types

Must remember:

Abstract types can be subtyped but not instantiated.
Concrete types cannot be subtyped.

Below are my code for some exercises:


using InteractiveUtils 
println("  ")
# symbol  <: means "is a subtype of"
#=
One particularly distinctive feature of Julia's type system is that concrete types may not subtype each other: 
all concrete types are final and may only have abstract types as their supertypes.
=#

#############################
# abstract type
#############################

abstract type Cat  end   ## this is how we define an "abstract type"
println("Type of Cat: ", typeof(Cat)) 

# Cat IS A SUBSET OF Any
abstract type Cat <: Any end
println("Supertype of Cat: ", supertype(Cat)) 

# chaton IS A SUBSET OF Cat 
struct chaton <: Cat
    name::String
end

# now we define an instance of chaton
a = chaton("yoyo")  # how its constructor works

println("a: $a, typeof a: ",  typeof(a)) 
println("  ")

#############################
# primitive Types
#############################
primitive type myReal16 <: Real 16 end  # 16 is bits number of this datatype 
primitive type myAny <: Any 32 end  # 32 is bits number of this datatype

println("Type of myReal16: ",  typeof(myReal16)) 
println("Supertype of myReal16: ",  supertype(myReal16)) 
println("Type of myAny: ",  typeof(myAny)) 
println("Supertype of myReal16: ",  supertype(myReal16)) 


#############################
# composite Types and mutable composite Types 
#############################
println("  ")

abstract type Bird end

## by default, a composite Type (struct) is immutable
# Duck IS A SUBSET OF Bird 
struct Duck <:  Bird
  age::Float64
  color # namely `::Any`
end

## we can use keyword mutable to specify a mutable struct
# mutableDuck IS A SUBSET OF Bird 
mutable struct mutableDuck <:  Bird
  age::Float64
  color # namely `::Any`
end

# constructor function by default takes the members of struct
lolo = Duck(12, "Yellow")
println("lolo : $lolo ")

## check if Duck instance is mutable 
try 
   donald = Duck(35,"orange") 
   donald.age = 45
   println("donald : $donald ")
catch e
    println(e)
end

## check if mutableDuck instance is mutable 
try 
   donald = mutableDuck(3.5,"orange") 
   donald.age = 45
catch e
    println(e)
end


# use typeof() to get constructor name
huahua = typeof(lolo)(6, "rouge") 
println("huahua : $huahua ")

### test using abstract Type for constructing an object
try 
   yoyo = Bird(3.5,"orange") 
catch e
    println("Error about yoyo: ", e)
end



struct Parrot <: Bird # Parrot IS A SUBSET OF Bird 
  age
  color
  sing::String
end

### constructor with several members' values fixed
Parrot(sing::String) = Parrot(13, "green", sing)
momo = Parrot("kuakuakua")
println("momo: $momo, Type of momo: ",  typeof(momo)) 

struct Swan <: Bird # Swan IS SUBTYPE OF Bird
  color
  Swan() = new("snowwhite")
  # Swans has only one constructor : Swan()
end
popo = Swan()

println("$popo, Type of popo: ",  typeof(popo)) 

println("  ")
#############################
# Parametric Abstract Types
#############################

abstract type Coordinate3D{T} end


b = Coordinate3D{Int64} <: Coordinate3D
println("b : $b")

struct Location3D{T} <: Coordinate3D{T}
           x::T
           y::T
           z::T
       end

pp = Location3D{Int32}(2, 5, 6)
println("pp : $pp, type:", typeof(pp))
println("pp.x : $(pp.x), pp.y: $(pp.y), pp.z: $(pp.z) ")
Enter fullscreen mode Exit fullscreen mode

Execution output:

Alt Text

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)