forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal-const-invariant.dd
588 lines (490 loc) · 10.7 KB
/
final-const-invariant.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
Ddoc
$(SPEC_S Final$(COMMA) Const$(COMMA) and Invariant,
$(P
Being able to specify what parts of variables data can change, and under
what conditions, can add greatly to the understandability of interfaces,
being able to analyse code for correctness, and improve code
generation.
)
$(P
With invariant, const and final, the programmer can carefully control
these attributes.
)
<h2>Invariant Storage Class</h2>
$(P
An invariant declaration cannot change, ever, and any data
that can be referenced through the invariant cannot ever
change. Initializers for invariant declarations can be placed into
ROM (Read Only Memory).
)
---
invariant int x = 3; // x is set to 3
invariant int y; // y is set to int.init, which is 0
x = 4; // error, x is invariant
y = 5; // error, y is invariant
---
$(P
The initializer for an invariant declaration must be evaluatable
at compile time:
)
---
int foo(int f) { return f * 3; }
int i = 5;
invariant int x = 3 * 4; // ok, 12
invariant int y = i + 1; // error, cannot evaluate at compile time
invariant int z = foo(2) + 1; // ok, foo(2) can be evaluated at compile time, 7
---
$(P
Data referred to by an invariant is also invariant:
)
---
invariant char[] s = "foo";
s[0] = 'a'; // error, invariant
---
$(P
An implementation is allowed to replace an instance of an invariant
declaration with the initializer for that declaration.
Therefore, it is not legal to take the address of an invariant:
)
---
invariant int i = 3;
invariant* p = &i; // error, cannot take address of invariant
---
$(P
Invariant members of a class or struct do not take up
any space in instances of those objects:
)
---
struct S
{ int x;
invariant int y;
}
writefln(S.sizeof); // prints 4, not 8
---
$(P
The type of an invariant declaration is itself invariant.
)
<h2>Const Storage Class</h2>
$(P
A const declaration is exactly like an invariant declaration,
with the following differences:
)
$(UL
$(LI Any data referenced by the const declaration cannot be
changed from the const declaration, but it might be changed
by other references to the same data.)
$(LI The type of a const declaration is itself const.)
)
<h2>Final Storage Class</h2>
$(P
A final declaration is one that, once initialized, can never
change its value.
)
---
final int x = 3;
x = 4; // error, x is final
---
$(P
Final declarations can be initialized either by an initializer,
or by a constructor:
)
---
final int x;
static this()
{
x = 4; // ok, can initialize final x inside constructor
x = 5; // still ok, because still in constructor
}
...
x = 6; // error, x is final
class C
{
final int s;
this()
{ s = 3; // ok, can initialize in constructor
}
}
---
$(P
Final declarations are stored and do take up space,
therefore their address can be taken.
)
$(P
Taking the address of a final variable of type T results in a
type that's const(T)*.
)
---
final int x = 3;
auto p = &x; // p is const(int)*
*p = 4; // error, *p is const
---
$(P
Final declarations are themselves neither invariant nor const.
)
---
int x = 4;
final int* p = &x;
p = null; // error, p is final
*p = 3; // ok, x is now 3
---
<h2>Invariant Type</h2>
$(P
Data that will never change its value can be typed as invariant.
The invariant keyword can be used as a $(I type constructor):
)
---
invariant(char)[] s = "hello";
---
$(P
The invariant applies to the type within the following parentheses.
So, while s can be assigned new values, the contents of s[] cannot
be:
)
---
s[0] = 'b'; // error, s[] is invariant
s = null; // ok, s itself is not invariant
---
$(P
Invariantness is transitive, meaning it applies to anything that
can be referenced from the invariant type:
)
---
invariant(char*)** p = ...;
p = ...; // ok, p is not final
*p = ...; // *p is not invariant
**p = ...; // error, **p is invariant
***p = ...; // error, ***p is invariant
---
$(P
The invariantness also only applies to what is referred to, not
the declaration itself:
)
---
invariant(char*) p = ...;
p = ...; // ok, invariant doesn't apply to p itself
*p = ...; // error, invariant applies to what p refers to
---
<h2>Creating Invariant Data</h2>
$(P
The first way is to use a literal that is already invariant,
such as string literals. String literals are always invariant.
)
---
auto s = "hello"; // s is invariant(char)[5]
char[] p = "world"; // error, cannot implicitly convert invariant
// to mutable
---
$(P
The second way is to cast data to invariant.
When doing so, it is up to the programmer to ensure that no
other mutable references to the same data exist.
)
---
char[] s = ...;
invariant(char)[] p = cast(invariant)s; // undefined behavior
invariant(char)[] p = cast(invariant)s.dup; // ok, unique reference
---
$(P
The .idup property is a convenient way to create an invariant
copy of an array:
)
---
auto p = s.idup;
p[0] = ...; // error, p[] is invariant
---
<h2>Removing Invariant With A Cast</h2>
$(P
The invariant type can be removed with a cast:
)
---
invariant int* p = ...;
int* q = cast(int*)p;
---
$(P
This does not mean, however, that one can change the data:
)
---
*q = 3; // allowed by compiler, but result is undefined behavior
---
$(P
The ability to cast away invariant-correctness is necessary in
some cases where the static typing is incorrect and not fixable, such
as when referencing code in a library one cannot change.
Casting is, as always, a blunt and effective instrument, and
when using it to cast away invariant-correctness, one must assume
the responsibility to ensure the invariantness of the data, as
the compiler will no longer be able to statically do so.
)
<h2>Invariant Doesn't Apply To Declared Symbols</h2>
$(P
Consider the struct:
)
---
struct S
{
int x;
int* p;
}
---
$(P
In order to be able to use structs as user-defined wrappers
for builtin types, it must be possible to declare a struct instance
as having its members be mutable, but what it refers to to
not be mutable. But all that's syntactically available is:
)
---
invariant(S) s;
---
$(P
Therefore, the invariant qualifier doesn't apply to the symbol
itself being declared. It only applies to anything indirectly
referenced by the symbol. Hence,
)
---
s.x = 3; // ok
*s.p = 3; // error, it's invariant
---
$(P
For consistency's sake, then this must apply generally:
)
---
int x;
invariant(int*) p;
p = cast(invariant)&x; // ok
*p = 3; // error, invariant
invariant(int) y;
y = 3; // ok
auto q = cast(invariant)&y; // q's type is invariant(int)*
*q = 4; // error, invariant
---
$(P
A similar situation applies to classes. Given:
)
---
class C
{
int x;
int* p;
}
invariant(C) c;
c = new C; // (1) ok
c.x = 3; // (2) error, invariant
*c.p = 4; // (3) error, invariant
---
$(P
Note that the c.x is an error, while the s.x is not. The reason is
that c is already a reference type - so the invariant does not
apply to c itself (1), but it does apply to what c refers to (2) and
anything transitively referred to (3).
)
<h2>Invariant Member Functions</h2>
$(P
Invariant member functions are guaranteed that the object
and anything referred to by the this reference is invariant.
They are declared as:
)
---
struct S
{ int x;
invariant void foo()
{
x = 4; // error, x is invariant
this.x = 4; // error, x is invariant
}
}
---
<h2>Const Type</h2>
$(P
Const types are like invariant types, except that const
forms a read-only $(I view) of data. Other aliases to that
same data may change it at any time.
)
<h2>Const Member Functions</h2>
$(P
Const member functions are functions that are not allowed to
change any part of the object through the member function's
this reference.
)
<h2>Implicit Conversions</h2>
$(P
Mutable and invariant types can be implicitly converted to const.
Mutable types cannot be implicitly converted to invariant,
and vice versa.
)
<h2>Comparing D Invariant, Const and Final with C++ Const</h2>
<table border=2 cellpadding=4 cellspacing=0 class="comp">
<caption>Final, Const, Invariant Comparison</caption>
<thead>
$(TR
$(TH Feature)
$(TH D)
$(TH C++98)
)
</thead>
<tbody>
$(TR
$(TD final keyword)
$(TD Yes)
$(TD No)
)
$(TR
$(TD const keyword)
$(TD Yes)
$(TD Yes)
)
$(TR
$(TD invariant keyword)
$(TD Yes)
$(TD No)
)
$(TR
$(TD const notation)
$(TD Functional:
---
// ptr to const ptr to const int
const(int*)* p;
---
)
$(TD Postfix:
$(CPPCODE
// ptr to const ptr to const int
const int *const *p;
)
)
)
$(TR
$(TD transitive const)
$(TD Yes:
---
const int** p; // const ptr to const ptr to const int
**p = 3; // error
---
)
$(TD No:
$(CPPCODE
int** const p; // const ptr to ptr to int
**p = 3; // ok
)
)
)
$(TR
$(TD cast away const)
$(TD Yes:
---
const(int)* p; // ptr to const int
int* q = cast(int*)p; // ok
---
)
$(TD Yes:
$(CPPCODE
const int* p; // ptr to const int
int* q = const_cast<int*>p; // ok
)
)
)
$(TR
$(TD modification after casting away const)
$(TD No:
---
const(int)* p; // ptr to const int
int* q = cast(int*)p;
*q = 3; // undefined behavior
---
)
$(TD Yes:
$(CPPCODE
const int* p; // ptr to const int
int* q = const_cast<int*>p;
*q = 3; // ok
)
)
)
$(TR
$(TD overloading of top level const)
$(TD No:
---
void foo(int x);
void foo(const int x); // error
---
)
$(TD No:
$(CPPCODE
void foo(int x);
void foo(const int x); // error
)
)
)
$(TR
$(TD aliasing of const with mutable)
$(TD Yes:
---
void foo(const int* x, int* y)
{
bar(*x); // bar(3)
*y = 4;
bar(*x); // bar(4)
}
...
int i = 3;
foo(&i, &i);
---
)
$(TD Yes:
$(CPPCODE
void foo(const int* x, int* y)
{
bar(*x); // bar(3)
*y = 4;
bar(*x); // bar(4)
}
...
int i = 3;
foo(&i, &i);
)
)
)
$(TR
$(TD aliasing of invariant with mutable)
$(TD Yes:
---
void foo(invariant int* x, int* y)
{
bar(*x); // bar(3)
*y = 4; // undefined behavior
bar(*x); // bar(??)
}
...
int i = 3;
foo(cast(invariant)&i, &i);
---
)
$(TD No invariants)
)
$(TR
$(TD type of string literal)
$(TD invariant(char)[])
$(TD const char*)
)
$(TR
$(TD implicit conversion of string literal to non-const)
$(TD not allowed)
$(TD allowed, but deprecated)
)
</tbody>
</table>
)
Macros:
TITLE=Final$(COMMA) Const$(COMMA) and Invariant
WIKI=FinalConstInvariant
NO=<td class="compNo">No</td>
NO1=<td class="compNo"><a href="$1">No</a></td>
YES=<td class="compYes">Yes</td>
YES1=<td class="compYes"><a href="$1">Yes</a></td>
D_CODE = <pre class="d_code2">$0</pre>
CPPCODE2 = <pre class="cppcode2">$0</pre>
ERROR = $(RED $(B error))
COMMA=,
META_KEYWORDS=D Programming Language, const,
final, invariant
META_DESCRIPTION=Comparison of const between the
D programming language, C++, and C++0x