Types

Type Assertion


In [41]:
(1+2)::AbstractFloat


TypeError: typeassert: expected AbstractFloat, got Int64

In [42]:
(1+2)::Int


Out[42]:
3

In [43]:
function foo() x::Int8 = 100
x
end


Out[43]:
foo (generic function with 1 method)

In [49]:
x = foo()
100
typeof(x)
Int8


Out[49]:
Int8

User Defined Types


In [34]:
workspace()
type Employee
           firstName::String
           lastName::String
           id::Int64
end

In [35]:
emp1=Employee("John", "Frusciante", 1)
println(emp1.firstName)
println(emp1.lastName)
println(emp1.id)


John
Frusciante
1

In [36]:
# User defined types can be changed

emp1.firstName="Frederick"
emp1.lastName="Chopin"
emp1


Out[36]:
Employee("Frederick","Chopin",1)

In [37]:
# Julia pass the objects to function by reference, so any changes you made to the object inside a function are visible 
#outside also
function changeEmployee(emp::Employee,firstName,lastName,id)
           emp.firstName=firstName
           emp.lastName=lastName
           emp.id=id
end

changeEmployee(emp1,"Django","Reinhardt",1234)
emp1


Out[37]:
Employee("Django","Reinhardt",1234)

In [38]:
#You can create immutable type using the keyword immutable

In [39]:
immutable Student
           id::Int64
           name::String
end

stud1=Student(1, "Bill")


Out[39]:
Student(1,"Bill")

In [40]:
stud1.id=5


type Student is immutable

In [ ]: