Changeset 1386
- Timestamp:
- 12/15/09 21:23:11 (15 years ago)
- Files:
-
- trunk/docsrc/const-faq.dd (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/docsrc/const-faq.dd
r833 r1386 10 10 11 11 $(ITEMR const, Why does D have const?) 12 12 $(ITEMR principles, What principles drove the D const design?) 13 13 $(ITEMR transitive-const, What is $(I transitive const)?) 14 14 $(ITEMR head-const, What is $(I head const)?) 15 15 $(ITEMR tail-const, What is $(I tail const)?) 16 16 $(ITEMR logical-const, What is $(I logical const)?) 17 17 $(ITEMR readonly, Why not use $(I readonly) to mean read only view?) 18 18 $(ITEMR java-const, Why did Java reject const?) 19 19 $(ITEMR cpp-const, How does const differ in C++?) 20 $(ITEMR invariant-strings, Why are strings i nvariant?)20 $(ITEMR invariant-strings, Why are strings immutable?) 21 21 $(ITEMR const-parameters, Why aren't function parameters const by default?) 22 22 $(ITEMR static-members, Are static class members covered by transitive const?) 23 $(ITEMR invariant, What is $(I i nvariant) good for?)23 $(ITEMR invariant, What is $(I immutable) good for?) 24 24 ) 25 25 26 26 $(ITEM const, Why does D have const?) 27 27 28 $(P People often express frustration with const and i nvariant28 $(P People often express frustration with const and immutable 29 29 in D 2.0 and wonder if it is worth it. 30 30 ) 31 31 32 32 $(OL 33 33 34 34 $(LI It makes function interfaces more self-documenting. Without 35 35 transitive const, for all pointer/reference parameters one must rely on 36 36 the documentation (which is always missing, out of date, or wrong). Note 37 37 that without transitivity, C++ const is nearly useless for such 38 38 self-documentation, which is why C++ programmers tend to rely on … … 194 194 $(P $(LINK http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4211070) 195 195 ) 196 196 197 197 $(ITEM cpp-const, How does const differ in C++?) 198 198 199 199 $(P C++ has a const system that is closer to D's than any other 200 200 language, but it still has huge differences:) 201 201 202 202 $(OL 203 203 $(LI const is not transitive) 204 $(LI no i nvariants)204 $(LI no immutables) 205 205 $(LI const objects can have mutable members) 206 206 $(LI const can be legally cast away and the data modified) 207 207 $(LI $(CODE const T) and $(CODE T) are not always distinct types) 208 208 ) 209 209 210 $(ITEM invariant-strings, Why are strings i nvariant?)211 212 $(P $(LINK2 http://dobbscodetalk.com/index.php?option=com_myblog&show=Invariant-Strings.html&Itemid=29, I nvariantStrings)210 $(ITEM invariant-strings, Why are strings immutable?) 211 212 $(P $(LINK2 http://dobbscodetalk.com/index.php?option=com_myblog&show=Invariant-Strings.html&Itemid=29, Immutable Strings) 213 213 ) 214 214 215 215 $(ITEM const-parameters, Why aren't function parameters const by default?) 216 216 217 217 $(P Since most (nearly all?) function parameters will not be modified, 218 218 it would seem to make sense to make them all const by default, 219 219 and one would have to specifically mark as mutable those that would 220 220 be changed. The problems with this are: 221 221 ) 222 222 … … 246 246 --- 247 247 248 248 $(ITEM static-members, Are static class members covered by transitive const?) 249 249 250 250 $(P A static class member is part of the global state of a program, 251 251 not part of the state of an object. Thus, a class having a mutable 252 252 static member does not violate the transitive constness of an object 253 253 of that class. 254 254 ) 255 255 256 $(ITEM invariant, What is $(I i nvariant) good for?)257 258 $(P I nvariantdata, once initialized, is never changed.256 $(ITEM invariant, What is $(I immutable) good for?) 257 258 $(P Immutable data, once initialized, is never changed. 259 259 This has many uses: 260 260 ) 261 261 262 262 $(UL 263 $(LI Access to i nvariantdata need not be synchronized263 $(LI Access to immutable data need not be synchronized 264 264 when multiple threads read it.) 265 265 $(LI Data races, tearing, sequential consistency, and 266 266 cache consistency are all non-issues when working with 267 i nvariantdata.)268 $(LI Pure functions can only accept i nvariantparameters.)267 immutable data.) 268 $(LI Pure functions can only accept immutable parameters.) 269 269 $(LI When doing a $(I deep copy) of a data structure, 270 the i nvariantportions need not be copied.)270 the immutable portions need not be copied.) 271 271 $(LI Invariance allows a large chunk of data to be treated 272 272 as a value type even if it is passed around by reference 273 273 (strings are the most common case of this).) 274 $(LI I nvarianttype provides more self-documenting information274 $(LI Immutable type provides more self-documenting information 275 275 to the programmer.) 276 $(LI I nvariantdata can be placed in hardware protected read-only276 $(LI Immutable data can be placed in hardware protected read-only 277 277 memory, or even in ROMs.) 278 $(LI If i nvariantdata does change, it is a sure sign of a memory278 $(LI If immutable data does change, it is a sure sign of a memory 279 279 corruption bug, and it is possible to automatically check for 280 280 such data integrity.) 281 $(LI I nvarianttypes provide for many program optimization281 $(LI Immutable types provide for many program optimization 282 282 opportunities.) 283 283 ) 284 284 285 $(P $(I const) acts as a bridge between the mutable and i nvariant285 $(P $(I const) acts as a bridge between the mutable and immutable 286 286 worlds, so a single function can be used to accept both types 287 287 of arguments.) 288 288 ) 289 289 290 290 Macros: 291 291 TITLE=const(FAQ) 292 292 WIKI=constFAQ 293 293 ITEMR=$(LI $(LINK2 #$1, $+)) 294 294 ITEM=<hr><h3><a name="$1">$+</a></h3>
