Difference between revisions of "W1412 Constructors"

From Coder Merlin
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[File:DRAFT ICON.png|DRAFT ICON]]
[[File:Louis-Emile Durandelle, The Eiffel Tower - State of the Construction, 1888.jpg|thumb|right|Louis-Emile Durandelle, The Eiffel Tower - State of the Construction, 1888]]
 
== Prerequisites ==
 
* [[W1215 Function Signatures]]
= Initialization =
* [[W1251 Parameter Passing]]


== Research ==
== Research ==
Line 12: Line 12:


== Experiment ==
== Experiment ==
Create a directory within your "project" directory.
Create a directory within your "Experiences" directory:
<syntaxhighlight lang="bash">
{{ConsoleLine|john-williams@codermerlin:~$|cd ~/Experiences}}
cd ~/projects
{{ConsoleLine|john-williams@codermerlin:~/Experiences$|mkdir W1412}}
mkdir project-1412
{{ConsoleLine|john-williams@codermerlin:~/Experiences$|cd W1412}}
cd project-1412
 
</syntaxhighlight>
Create a new swift project:
{{ConsoleLine|john-williams@codermerlin:~/Experiences/W1412$|swift-init}}


Edit a new file named "main.swift"
Edit a new file named {{Pathname|main.swift}}:
<syntaxhighlight lang="bash">
{{ConsoleLine|john-williams@codermerlin:~/Experiences/W1412$|emacs main.swift}}
emacs main.swift
</syntaxhighlight>


For this project you'll repeatedly edit the same file.  It's probably easiest to test this file ''within emacs'' by typing:
For this project you'll repeatedly edit the same file.  It's probably easiest to test this file ''within emacs'' by typing:
'''M-&''' (async-shell-command) and observing the results.  For each question, be sure to '''understand''' the behavior before proceeding to the next question.  It will be helpful to '''record''' your answers and reasoning for later review.
{{Key|F5}} {{Key|r}} and observing the results.  For each question, be sure to '''understand''' the behavior before proceeding to the next question.  It will be helpful to '''record''' your answers and reasoning for later review.
 
{{notice|[[File:Oxygen480-actions-help-hint.svg|frameless|30px]]|Helpful hint:  The '''async-shell-command''' can be executed with ''Alt-Shift-7''.  The command to execute each time is: ''swift main.swift''}}


<quiz shuffleanswers=true display=simple>
<quiz shuffleanswers=true display=simple>
Line 319: Line 316:
</quiz>
</quiz>


== Key Concepts ==
{{KeyConcepts|
* '''Initialization''' is the process of preparing an instance of a class, structure, or enumeration type for use.
* '''Initialization''' is the process of preparing an instance of a class, structure, or enumeration type for use.
** Initialization '''requires''' that we '''provide an initial value for each stored property'''.
** Initialization '''requires''' that we '''provide an initial value for each stored property'''.
Line 331: Line 328:
* Because the initializer must be called ''init'', '''argument labels''' play a crucial role for specifying the correct initializer.
* Because the initializer must be called ''init'', '''argument labels''' play a crucial role for specifying the correct initializer.
* Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified.
* Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified.
}}

Latest revision as of 21:24, 22 March 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Louis-Emile Durandelle, The Eiffel Tower - State of the Construction, 1888

Prerequisites[edit]

Research[edit]

Initialization is the process of preparing an instance of a class, structure, or enumeration type for use. It requires that we provide an initial value for each stored property. We may also perform any other additional initialization that we require.

Experiment[edit]

Create a directory within your "Experiences" directory:

john-williams@codermerlin:~$ cd ~/Experiences

john-williams@codermerlin:~/Experiences$ mkdir W1412

john-williams@codermerlin:~/Experiences$ cd W1412

Create a new swift project:

john-williams@codermerlin:~/Experiences/W1412$ swift-init

Edit a new file named main.swift:

john-williams@codermerlin:~/Experiences/W1412$ emacs main.swift

For this project you'll repeatedly edit the same file. It's probably easiest to test this file within emacs by typing: F5 r and observing the results. For each question, be sure to understand the behavior before proceeding to the next question. It will be helpful to record your answers and reasoning for later review.

1

class Color {
    let red : UInt8
    let green : UInt8
    let blue : UInt8
} //

let color = Color()

What are the property values after initialization?

red:255, green:255, blue:255
Compile or runtime error
red:nil, green:nil, blue:nil
red:0, green:0, blue:0

2

class Color {
    let red : UInt8
    let green : UInt8
    let blue : UInt8

    init() {
        red = 255
        green = 255
        blue = 255
    } //
} //

let color = Color()

What are the property values after initialization?

red:0, green:0, blue:0
red:255, green:255, blue:255
red:nil, green:nil, blue:nil
Compile or runtime error

3

class Color {
    let red : UInt8
    let green : UInt8
    let blue : UInt8

    init() {
        red = 255
        green = 255
        blue = 255

        red = 0
    } //
} //

let color = Color()

What are the property values after initialization?

red:nil, green:nil, blue:nil
Compile or runtime error
red:255, green:255, blue:255
red:0, green:255, blue:255

4

class Color {
    var red : UInt8
    var green : UInt8
    var blue : UInt8

    init() {
        red = 255
        green = 255
        blue = 255

        red = 0
    } //
} //

let color = Color()

What are the property values after initialization?

red:nil, green:nil, blue:nil
red:255, green:255, blue:255
red:0, green:255, blue:255
Compile or runtime error

5

class Color {
    let red : UInt8? = nil
    let green : UInt8? = nil
    let blue : UInt8? = nil
} //

let color = Color()

What are the property values after initialization?

red:255, green:255, blue:255
red:0, green:255, blue:255
red:nil, green:nil, blue:nil
Compile or runtime error

6

class Color {
    let red : UInt8? = nil
    let green : UInt8? = nil
    let blue : UInt8? = nil

    init() {
    } //
} //

let color = Color(red:0, green:255, blue:255)

What are the property values after initialization?

Compile or runtime error
red:nil, green:nil, blue:nil
red:0, green:255, blue:255
red:255, green:255, blue:255

7

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init() {
    } //

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color(red:0, green:255, blue:255)

What are the property values after initialization?

Compile or runtime error
red:255, green:255, blue:255
red:127, green:127, blue:127
red:0, green:255, blue:255

8

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color(red:0, green:255, blue:255)

What are the property values after initialization?

red:127, green:127, blue:127
Compile or runtime error
red:0, green:255, blue:255
red:255, green:255, blue:255

9

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color(white:127)

What are the property values after initialization?

red:127, green:127, blue:127
red:255, green:255, blue:255
Compile or runtime error
red:0, green:255, blue:255

10

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init() {
        self.red = 0
        self.green = 255
        self.blue = 255
    } //

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color()

What are the property values after initialization?

Compile or runtime error
red:127, green:127, blue:127
red:255, green:255, blue:255
red:0, green:255, blue:255

11

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    func init() {
        self.red = 0
        self.green = 255
        self.blue = 255
    } //

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color()

What are the property values after initialization?

red:255, green:255, blue:255
red:0, green:255, blue:255
red:127, green:127, blue:127
Compile or runtime error


Key ConceptsKeyConceptsIcon.png
  • Initialization is the process of preparing an instance of a class, structure, or enumeration type for use.
    • Initialization requires that we provide an initial value for each stored property.
  • Initialization is handled by defining initializers.
  • Unlike functions and methods, initializer do not return a value.
  • Initial values for stored properties may be:
    • Set within an initializer, or
    • by setting a default value value when the property is defined
  • The simplest initializer is simply defined as init(), with no parameters. Note that is must be called init.
  • As with other functions and methods, a parameter list may be provided.
  • Because the initializer must be called init, argument labels play a crucial role for specifying the correct initializer.
  • Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified.