Skip to content

Commit 14c9142

Browse files
committed
Finish revising the introduction of optionals.
1 parent fe8169b commit 14c9142

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

TSPL.docc/LanguageGuide/TheBasics.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,22 +1190,22 @@ For more information, see <doc:Functions#Functions-with-Multiple-Return-Values>.
11901190

11911191
You use *optionals* in situations where a value may be absent.
11921192
An optional represents two possibilities:
1193-
Either there *is* a value, and you can unwrap the optional to access that value,
1193+
Either there *is* a value of a specified type,
1194+
and you can unwrap the optional to access that value,
11941195
or there *isn't* a value at all.
11951196

1196-
For example,
1197+
As an example of a value that might be missing,
11971198
Swift's `Int` type has an initializer
11981199
that tries to convert a `String` value into an `Int` value.
1199-
However, not every string can be converted into an integer.
1200+
However, only some strings can be converted into integers.
12001201
The string `"123"` can be converted into the numeric value `123`,
1201-
but the string `"hello, world"` doesn't have an obvious numeric value to convert to.
1202-
1202+
but the string `"hello, world"` doesn't have a corresponding numeric value.
12031203
The example below uses the initializer to try to convert a `String` into an `Int`:
12041204

12051205
```swift
12061206
let possibleNumber = "123"
12071207
let convertedNumber = Int(possibleNumber)
1208-
// convertedNumber is inferred to be of type "Int?", or "optional Int"
1208+
// The type of convertedNumber is "optional Int"
12091209
```
12101210

12111211
<!--
@@ -1220,14 +1220,16 @@ let convertedNumber = Int(possibleNumber)
12201220
```
12211221
-->
12221222

1223-
Because the initializer might fail,
1223+
Because the initializer in the code above might fail,
12241224
it returns an *optional* `Int`, rather than an `Int`.
1225-
An optional `Int` is written as `Int?`,
1226-
with a question mark to indicate that the value it contains is optional,
1227-
meaning that it might contain *some* `Int` value,
1228-
or it might contain *no value at all*.
1229-
It can't contain anything else, such as a `Bool` value or a `String` value;
1230-
it's either an `Int`, or it's nothing at all.
1225+
1226+
To write an optional type,
1227+
you write a question mark (`?`)
1228+
after the name of the type that the optional contains ---
1229+
for example, the type of an optional `Int` is `Int?`.
1230+
An optional `Int` always contains
1231+
either some `Int` value or no value at all.
1232+
It can't contain anything else, like a `Bool` value or a `String` value.
12311233

12321234
### nil
12331235

0 commit comments

Comments
 (0)