Variable types

Introduction

  • Just as we have different numerical value types, i.e. $ 3 $ is an integer and $ \frac{3}{4} $ is a rational number (fraction), so to do computer variables (well actually the object inside of a variable)
  • As the lesson name indicates, these are called types
  • Everytime a variable is created, the value it holds is of some type
    • If we say x = 3, then the variable name is x and it holds an integer value
    • This value that it holds is called an object and all values in Julia are true objects
  • Julia has a dynamic type system
  • This means that you do not have to explicitly declare a data type (tell the computer what type of data a variable will hold)
    • In languages with a static type system you will have to use something like: int x = 3; to declare the fact that the variable x must hold integer values
  • The advantage of declaring a type is that the computer can hold the correct memory space and this make execution faster
  • Fortunately, Julia also allows for declaring a type
  • Remember, it is the object that has a type, the variable is just a container or a name connected to values
  • The types that are available in Julia are structured in a hierarchy

Finding out the types of common values


In [1]:
#The type of the value 3
typeof(3)


Out[1]:
Int64

In [2]:
#The type of the value 3.0
typeof(3.0)


Out[2]:
Float64

In [3]:
#The type of the value pi
typeof(pi)


Out[3]:
MathConst{:π} (constructor with 1 method)

In [4]:
#The type of the value 3 // 4
typeof(3 // 4)


Out[4]:
Rational{Int64} (constructor with 1 method)

In [5]:
#The type of the value "Julia"
typeof("Julia")


Out[5]:
ASCIIString (constructor with 2 methods)

Supertypes

  • Supertypes indicates the parent of a type

In [6]:
#What is the supertype (parent) of the Float64 type?
super(Float64)


Out[6]:
FloatingPoint

In [7]:
#What is the supertype of the FloatingPoint type?
super(FloatingPoint)


Out[7]:
Real

In [8]:
#What is the sypertype of the Real type?
super(Real)


Out[8]:
Number

In [9]:
#What is the supertype of the Number type?
super(Number)


Out[9]:
Any

Subtypes

  • Subtypes indicate the children of a parent type

In [10]:
#What are the children of the type Any?
subtypes(Any)


Out[10]:
369-element Array{Any,1}:
 Abs2MinusFun                                                                
 AbsoluteBoundingBox                                                         
 AbstractAlphaColorValue{C<:ColorValue{T},T<:Real}                           
 AbstractArray{T,N}                                                          
 AbstractCmd                                                                 
 AbstractDataFrame                                                           
 AbstractEntry                                                               
 AbstractFormatter                                                           
 AbstractHeap{VT}                                                            
 AbstractHistogram{T<:Real,N,E}                                              
 AbstractIndex                                                               
 AbstractPDMat                                                               
 AbstractREPL                                                                
 ⋮                                                                           
 Worker                                                                      
 ZeroOffsetVector                                                            
 ZeroVector{T}                                                               
 Zip2{I1,I2}                                                                 
 Zip{I<:(Any...,)}                                                           
 c_CholmodDense{T<:Union(Complex{Float64},Float64)}                          
 c_CholmodFactor{Tv<:Union(Complex{Float64},Float64),Ti<:Union(Int64,Int32)} 
 c_CholmodSparse{Tv<:Union(Complex{Float64},Float64),Ti<:Union(Int64,Int32)} 
 c_CholmodTriplet{Tv<:Union(Complex{Float64},Float64),Ti<:Union(Int64,Int32)}
 dl_phdr_info                                                                
 xtab{T}                                                                     
 z_stream                                                                    

In [11]:
#What are the subtypes of the Number type?
subtypes(Number)


Out[11]:
4-element Array{Any,1}:
 Complex{T<:Real}
 Dual4{T<:Real}  
 Dual{T<:Real}   
 Real            

In [12]:
#What are the subtypes of the Complex type?
subtypes(Complex)


Out[12]:
0-element Array{Any,1}

In [13]:
#What are the subtypes of the Real type?
subtypes(Real)


Out[13]:
5-element Array{Any,1}:
 FixedPoint          
 FloatingPoint       
 Integer             
 MathConst{sym}      
 Rational{T<:Integer}

In [14]:
#What are the subtypes of the FloatingPoint type?
subtypes(FloatingPoint)


Out[14]:
4-element Array{Any,1}:
 BigFloat
 Float16 
 Float32 
 Float64 

In [15]:
#What are the subtypes of the Integer type?
subtypes(Integer)


Out[15]:
5-element Array{Any,1}:
 BigInt  
 Bool    
 Char    
 Signed  
 Unsigned

Abstract versus concrete types

  • The bottom of this hierarchy contains the concrete types
  • The are the ones that can hold objects
  • They inherit from abstract types
  • Abstract type can not be instantiated
    • That is fancy speak for the fact that you cannot create an instance of an abstract type, i.e. f(x::Real) = ...

The size of types

  • A computer has a finite memory capacity
  • Code is placed in memory for interaction with the processing unit
  • An object is allocate space in memory and that space is dependent on its type
  • The space determines the minimum and maximum values a type can represent
  • Julia can tell use through the use of the typemin() and typemax() functions

In [28]:
#What is the minimum value an Int8 type object can hold?
typemin(Int8)


Out[28]:
-128

In [30]:
#What is the maximum value a Int16 type ofject can hold?
typemax(Int16)


Out[30]:
32767

Creating your own abstract and concrete types


In [31]:
#Creating an new abstract type called AbstractTypeOne using the abstract keyword
abstract AbstractTypeOne

In [32]:
#This new abstract type has an automatic supertype
super(AbstractTypeOne)


Out[32]:
Any

In [33]:
#Creating an abstarct type under a specific supertypes using: abstract AbstractTypeTwo <: Number
abstract AbstractTypeTwo <: Number

In [34]:
#Checking using super(AbstractTypeTwo)
super(AbstractTypeTwo)


Out[34]:
Number

In [36]:
#Creating a concrete type with two fields:
#type ConcreteTypeOne <: AbstractTypeOne
# fieldone
# fieldtwo::Int
#end
type ConcreteType <: AbstractTypeOne
    fieldone
    fieldtwo::Int
end

In [38]:
#Since it is a concrete type it can be instantiated:
#var1 = ConcreteTypeOne("Hi!", 101)
var1 = ConcreteType("Hi!", 101)


Out[38]:
ConcreteType("Hi!",101)

In [39]:
#Accessing the fields of var1 with dot notation
var1.fieldone


Out[39]:
"Hi!"

In [40]:
#Since the type of fieldone was not specified we can check the type of var1.fieldone with typeof()
typeof(var1.fieldone)


Out[40]:
ASCIIString (constructor with 2 methods)

Conclusion

  • There is a lot more to know about types
  • We'll come across those topics in future lessons as we learn about arrays and functions

In [ ]: