I'm trying to learn Julia and am reading a book that shows the following two code example in a chapter about composite types:
1.
type Points
x::Int64
y::Int64
z::Int64
end
2.
struct Point
x::Int
y::Int
z::Int
end
The book however does not explain, when to use struct
and when to use type
.
What is the difference?
A struct data type represents a collection of elements of different data types. A struct data type has an associated schema that defines the structure of the data. To pass, generate, or process struct data, assign struct data type to ports.
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit.
Generally speaking, objects bring the full object oriented functionality (methods, data, virtual functions, inheritance, etc, etc) whereas structs are just organized memory. Structs may or may not have support for methods / functions, but they generally won't support inheritance and other full OOP features.
Structs are value types and can be used to create objects that behave like built-in types. Structs share many features with classes but with the following limitations as compared to classes. Struct cannot have a default constructor (a constructor without parameters) or a destructor.
This is quite a mess in your sources, since it mixes different meanings from non-compatible epochs in the history of the language.
type
or immutable
, where type
was used for mutable types (there also was bitstype
for what is now called "primitive types").mutable struct
and struct
for the sampe purposes (and, consistently with it, primitive type
and abstract type
).So, basically the names have changed so that all ways to define types becomes a bit more consistent, and immutable structs have become the "unmarked" case.
"Mutable" in this context means that you can't reassign a field (p.x = 3
). It does not imply that the contents of a field cannot be changed, it they happen to be mutable (something.v[1] = 2
will also work if something
is an immutable type!).
As argued by @phipsgabler you are mixing code from different versions of Julia.
On this specific topic you can look also on my "Julia concise tutorial", on the topic of "Custom Structures".
The same topic, extended, is available in the "Julia Quick Syntax reference" book (Apress, 2019) from which I report an except from the unedited source (still full of English errors, but you should get the point..):
In Chapter 2 "Data Types and Structures" we discussed built-in types, including containers. In this chapter we move to explain how to create user-defined types.
Let's be clear about these two terms and what they mean in the Julia language context.
As type of an object we mean, as in plain English, the set of characteristics that an object can be described with. For example an object of type sheet can be described with its dimensions, weight, colour..
All values in Julia are true objects belonging to a given type (they are individual "instances" of the given type).
Julia types include so called primitive types made of just a fixed amount of bits (like all numerical types.. Int64
, Float64
, but also Char
..) and composite types or structures, where the set of characteristics of the object is described trough multiple fields and a variable number of bits.
Both structures and primitive-type can be user-defined and are hierarchically organised. Structures roughly correspond to what classes are known in other languages.
A user-defined primitive type is defined with the keyword primitive type
and just its name and the number of bits it requires:
primitive type [name] [bits] end
For example:
primitive type My10KBBuffer 81920 end
IMPORTANT: A (current) limitation of Julia is that the number of bits must be a multiple of 8 below 8388608.
Optionally a parent type can be specified:
primitive type [name] <: [supertype] [bits] end
Note that the internal representation of two user-defined types with the same number of bits is exactly the same. The only thing that would change is their names, but that's an important difference: it's the way that functions are defined to act when given objects of these types are passed as arguments that changes, i.e. it's the usage across the program of named types that distinguish them rather than their implementation.
In a similar way of primitive types, to define a structure we use the keyword mutable struct
, give the structure a name, specify the fields, and close the definition with the end
keyword:
mutable struct MyOwnType
field1
field2::String
field3::Int64
end
Note that while you can optionally define each individual field to be of a given type (e.g. field3::Int64) you can't define fields to be subtypes of a given type (e.g. field3<:Number
). In order to do that you can however use templates in the structure definition:
mutable struct MyOwnType{T<:Number}
field1
field2::String
field3::T
end
Using templates, the definition of the structure is dynamically created the first time an object whose field3 is of type T
is constructed.
The type with whom you annotate the individual fields can be either a primitive one (like in the example above) or a reference to an other structure (see later for an example). +
Note also that, differently from other high-level languages (e.g Python), you can't add or remove fields from a structure after you firstly defined it. If you need this functionality use <> instead, but be aware that you will trade-off this flexibility with worse performances.
At the opposite, to gain performances (but again trading up with flexibility), you can omit the mutable
keyword in front of struct
.
This will constraint that once an object of that type has been created, its fields can no longer be changed (i.e., structures are immutable by default).
Note that mutable objects -as arrays- remain themselves mutable also in a immutable structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With