Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

Parametric types and methods


An array can take elements of different types, so, we can have, for example, arrays of the following types: Array{Int64,1}, Array{Int8,1}, Array{Float64,1}, or Array{ASCIIString, 1}, and so on. That is why an Array is a parametric type; its elements can be of any arbitrary type T, written as Array{T, 1}.

In general, types can take type parameters, so that type declarations actually introduce a whole family of new types. Returning to the Point example of the previous section, we can generalize it to the following:

   # see the code in Chapter 6\parametric.jl
type Point{T}
  x::T
  y::T
end

(This is conceptually similar to generic types in Java or templates in C++).

This abstract type creates a whole family of new possible concrete types (but they are only compiled as needed at runtime), such as Point{Int64}, Point{Float64}, and Point{String}.

These are all subtypes of Point: issubtype(Point{String}, Point) that return true. However, this is not the case when comparing...