Ch 15 - Structs and Objects Flashcards
Think Julia
What is a: Composite Types
A programmer-defined composite type is also called a struct. The struct definition for a point looks like this:
struct Point
….x
….y
end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5756-5757). Kindle Edition.
Think Julia
What goes inside of a struct?
**struct Point
….x**
….y
end
The header indicates that the new struct is called Point. The body defines the attributes or fields of the struct. The Point struct has two fields: x and y.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5757-5761). Kindle Edition.
Think Julia
What is a: instantiation
Creating a new object is called instantiation, and the object is an instance of the type.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5769-5773). Kindle Edition.
Think Julia
How can you get the values of the fields of a struct?
You can get the values of the fields using** . notation**:
julia > x = p.x
3.0
julia > p.y
4.0
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5781-5787). Kindle Edition.
Think Julia
By default are structs mutable or immutable?
Structs are, however, by default immutable; after construction the fields cannot change value:
julia > p.y = 1.0
ERROR: setfield! immutable struct of type Point cannot be changed
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5796-5801). Kindle Edition.
Think Julia
How can a struct be made mutable?
Where required, mutable composite types can be declared with the keyword mutable struct. Here is the definition of a mutable point:
mutable struct MPoint
….x
….y
end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5806-5811). Kindle Edition.
Think Julia
What is a: embedded?
struct Rectangle
….width
….height
….corner
end
julia > origin = MPoint( 0.0, 0.0) #MPoint is a struct
MPoint( 0.0, 0.0)
julia > box = Rectangle( 100.0, 200.0, origin)
Rectangle( 100.0, 200.0, MPoint( 0.0, 0.0))
An object that is a field of another object is embedded. Because the corner attribute refers to a mutable object, the latter is drawn outside the Rectangle object.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5846-5848). Kindle Edition.
Think Julia
Can a instance of a struct be passed as an argument?
You can pass an instance as an argument in the usual way. For example:
function printpoint( p)
….println(“( $( p.x), $( p.y))”)
end
printpoint takes a Point as an argument and displays it in mathematical notation. To invoke it, you can pass p as an argument: julia > printpoint( blank) (3.0, 4.0)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5851-5862). Kindle Edition.
Think Julia
Can an instance of a struct be a returned item?
Instances as Return Values
Functions can return instances. For example, findcenter takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the rectangle:
function findcenter( rect)
….Point( rect.corner.x + rect.width / 2, rect.corner.y + rect.height / 2)
end
The expression rect.corner.x means, “Go to the object rect refers to and select the field named corner; then go to that object and select the field named x.” Here is an example that passes box as an argument and assigns the resulting Point to center:
julia > center = findcenter( box)
Point( 51.0, 102.0)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5912-5926). Kindle Edition.
Think Julia
Why should aliasing be avoided?
Aliasing can make a program difficult to read because changes in one place might have unexpected effects in another place. It is hard to keep track of all the variables that might refer to a given object.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5928-5931). Kindle Edition.
Think Julia
What is an alternative to aliasing?
Copying an object is often an alternative to aliasing. Julia provides a function called deepcopy that performs a deep copy and can duplicate any object, including the contents of any embedded objects:
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5931-5933). Kindle Edition.
Think Julia
What is a: deepcopy
Copying an object is often an alternative to aliasing. Julia provides a function called deepcopy that performs a deep copy and can duplicate any object, including the contents of any embedded objects:
julia > p1 = MPoint( 3.0, 4.0)
MPoint( 3.0, 4.0)
julia > p2 = deepcopy( p1)
MPoint( 3.0, 4.0)
julia > p1 ≡ p2
false
julia > p1 = = p2
false
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5931-5940). Kindle Edition.
Think Julia
What is a: isa
You can also use isa to check whether an object is an instance of a type:
julia > p isa Point
true
If you are not sure whether an object has a particular attribute, you can use the built-in function fieldnames:
julia > fieldnames( Point)
(: x, :y)
or the function isdefined:
julia > isdefined( p, :x)
true
julia > isdefined( p, :z)
false
The first argument can be any object; the second argument is a symbol, :, followed by the name of the field.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5967-5985). Kindle Edition.
Think Julia
What is a: fieldnames()
You can also use isa to check whether an object is an instance of a type:
julia > p isa Point
true
If you are not sure whether an object has a particular attribute, you can use the built-in function fieldnames:
julia > fieldnames( Point)
(: x, :y)
or the function isdefined:
julia > isdefined( p, :x)
true
julia > isdefined( p, :z)
false
The first argument can be any object; the second argument is a symbol, :, followed by the name of the field.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5967-5985). Kindle Edition.
Think Julia
What is a: isdefined()
You can also use isa to check whether an object is an instance of a type:
julia > p isa Point
true
If you are not sure whether an object has a particular attribute, you can use the built-in function fieldnames:
julia > fieldnames( Point)
(: x, :y)
or the function isdefined:
julia > isdefined( p, :x)
true
julia > isdefined( p, :z)
false
The first argument can be any object; the second argument is a symbol, :, followed by the name of the field.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5967-5985). Kindle Edition.