Ch 15 - Structs and Objects Flashcards

1
Q

Think Julia

What is a: Composite Types

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Think Julia

What goes inside of a struct?

A

**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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Think Julia

What is a: instantiation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Think Julia

How can you get the values of the fields of a struct?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Think Julia

By default are structs mutable or immutable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Think Julia

How can a struct be made mutable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Think Julia

What is a: embedded?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Think Julia

Can a instance of a struct be passed as an argument?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Think Julia

Can an instance of a struct be a returned item?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Think Julia

Why should aliasing be avoided?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Think Julia

What is an alternative to aliasing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Think Julia

What is a: deepcopy

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Think Julia

What is a: isa

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Think Julia

What is a: fieldnames()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Think Julia

What is a: isdefined()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Think Julia

What does the ‘ : ‘ symbole do in the functions fieldnames() and isdefined()?

A

The : indecates that a fieldname follows.