Difference between revisions of "W1035 Not a Value"

From Coder Merlin
(Created page with "thumb|link=|Paris, Notre Dame, Kilometer Null == Prerequisites == * W1031 Positive Integers * W1032 Negati...")
 
Line 5: Line 5:
* [[W1033 Character Encoding]]
* [[W1033 Character Encoding]]
== Introduction ==
== Introduction ==
There's often a need to represent the ''absence'' of a value.
There's often a need to represent the ''absence'' of a value. For example, consider a database tracking students' first name, middle initial, and last name.  Not all students have a middle initial.  So what value should we store in a variable representing a student's middle initial if this particular student lacks a middle initial? 
 
The name of this special value, representing ''non-existence'', varies from language to language.  In Swift, it's called '''nil'''.
 
== Nil ==
Nil represents the absence of value ''of the specified type''.  We indicate that we intent to accept such ''nil'' values by specifying the type explicitly and suffixing the type name with a question mark.  For example, in the aforementioned case, rather than specify the type as:
<syntaxhighlight lang="swift">
let middleName : Character
</syntaxhighlight>
we use:
<syntaxhighlight lang="swift">
let middleName : Character?
</syntaxhighlight>
 
== Topic Headers ==
== Topic Headers ==
== Key Concepts ==
== Key Concepts ==
== Exercises ==
== Exercises ==
== References ==
== References ==

Revision as of 22:16, 7 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Paris, Notre Dame, Kilometer Null

Prerequisites[edit]

Introduction[edit]

There's often a need to represent the absence of a value. For example, consider a database tracking students' first name, middle initial, and last name. Not all students have a middle initial. So what value should we store in a variable representing a student's middle initial if this particular student lacks a middle initial?

The name of this special value, representing non-existence, varies from language to language. In Swift, it's called nil.

Nil[edit]

Nil represents the absence of value of the specified type. We indicate that we intent to accept such nil values by specifying the type explicitly and suffixing the type name with a question mark. For example, in the aforementioned case, rather than specify the type as:

let middleName : Character

we use:

let middleName : Character?

Topic Headers[edit]

Key Concepts[edit]

Exercises[edit]

References[edit]