In Julia, as a developer you can define your own types to structure data used in applications. For example, if you need to represent points in a three-dimensional space, you can define a type Point
, as follows:
# see the code in Chapter 6\user_defined.jl:
mutable struct Point
x::Float64
y::Float64
z::Float64
end
mutable
here means that Point
values can be modified. If your type values cannot be changed, simply use struct
.
The type Point
is a concrete type. Objects of this type can be created as p1 = Point(2, 4, 1.3)
, and it has no subtypes: typeof(p1)
returns Point (constructor with 2 methods)
, subtypes(Point)
returns 0-element Array{Any,1}
.
Such a user-defined type is composed of a set of named fields with an optional type annotation; that's why it is a composite type, and its type is also DataType
. If the type of a named field is not given, then it is Any
. A composite type is similar to struct
in C, or a class without methods in Java.
Unlike...