Skip to content

Commit ae8d00a

Browse files
committed
Move DI discussion to reference & clean up.
Incorporates edits from Chuck Toporek <chuck_toporek@apple.com>. Fixes: rdar://115848515
1 parent e2c0334 commit ae8d00a

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

TSPL.docc/LanguageGuide/TheBasics.md

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ because this value must be incremented after each failed login attempt.
8888

8989
If a stored value in your code won't change,
9090
always declare it as a constant with the `let` keyword.
91-
Use variables only for storing values that need to be able to change.
91+
Use variables only for storing values that change.
9292

9393
When you declare a constant or a variable,
9494
you can give it a value as part of that declaration,
@@ -135,28 +135,14 @@ it has a value of 100;
135135
in any other environment, its value is 10.
136136
Both branches of the `if` statement
137137
initialize `maximumNumberOfLoginAttempts` with some value,
138-
which guarantees that the constant always gets a value.
138+
guaranteeing that the constant always gets a value.
139139

140140
When you define a constant or variable
141141
without giving it a value,
142-
Swift analyzes your code at compile time
143-
and confirms that all possible paths of execution
144-
are guaranteed to set a value before reading a value.
145-
This analysis is called *definitive initialization*.
146-
147-
> Note:
148-
> Definitive initialization
149-
> can't construct proofs that require domain knowledge,
150-
> and its ability to track state across conditionals is finite.
151-
> If you can determine that a value is always set,
152-
> but Swift can't prove this is the case,
153-
> try simplifying the conditionals or use a variable instead.
154-
155-
<!--
156-
In the most general case,
157-
DI reduces to the halting problem,
158-
as shown by Rice's theorem.
159-
-->
142+
Swift analyzes your code to make sure you set a value
143+
before the first time you read its value.
144+
For more information about this analysis,
145+
see <doc:Declarations#Constant-Declaration>.
160146

161147
You can declare multiple constants or multiple variables on a single line,
162148
separated by commas:

TSPL.docc/ReferenceManual/Declarations.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ as long as it's guaranteed to have a value set
156156
before the first time its value is read.
157157
If the compiler can prove that the constant's value is never read,
158158
the constant isn't required to have a value set at all.
159+
This analysis is called *definite initialization* ---
160+
the compiler proves that a value is definitely set before being read.
161+
162+
> Note:
163+
> Definite initialization
164+
> can't construct proofs that require domain knowledge,
165+
> and its ability to track state across conditionals has a limit.
166+
> If you can determine that constant always has a value set,
167+
> but the compiler can't prove this is the case,
168+
> try simplifying the code paths that set the value,
169+
> or use a variable declaration instead.
170+
171+
<!--
172+
In the most general case,
173+
DI reduces to the halting problem,
174+
as shown by Rice's theorem.
175+
-->
176+
159177
When a constant declaration occurs in the context of a class or structure
160178
declaration, it's considered a *constant property*.
161179
Constant declarations aren't computed properties and therefore don't have getters
@@ -277,6 +295,9 @@ That said, if no initializer *expression* is present,
277295
the variable declaration must include an explicit type annotation (`:` *type*).
278296

279297
As with constant declarations,
298+
if a variable declaration omits the initializer *expression*,
299+
the variable must have a value set before the first time it is read.
300+
Also like constant declarations,
280301
if the *variable name* is a tuple pattern,
281302
the name of each item in the tuple is bound to the corresponding value
282303
in the initializer *expression*.

0 commit comments

Comments
 (0)