| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(D_S Overview, |
|---|
| 4 |
|
|---|
| 5 |
$(SECTION2 What is D?, |
|---|
| 6 |
|
|---|
| 7 |
$(P D is a general purpose systems and applications programming language. |
|---|
| 8 |
It is a high level language, but retains the ability |
|---|
| 9 |
to write high performance code and interface directly with the |
|---|
| 10 |
operating system |
|---|
| 11 |
<acronym title="Application Programming Interface">API</acronym>'s |
|---|
| 12 |
and with hardware. |
|---|
| 13 |
D is well suited to writing medium to large scale |
|---|
| 14 |
million line programs with teams of developers. D is easy |
|---|
| 15 |
to learn, provides many capabilities to aid the programmer, |
|---|
| 16 |
and is well suited to aggressive compiler optimization technology. |
|---|
| 17 |
|
|---|
| 18 |
<img src="../d3.png" border=0 align=right alt="D Man"> |
|---|
| 19 |
) |
|---|
| 20 |
|
|---|
| 21 |
$(P D is not a scripting language, nor an interpreted language. |
|---|
| 22 |
It doesn't |
|---|
| 23 |
come with a <acronym title="Virtual Machine">VM</acronym>, |
|---|
| 24 |
a religion, or an overriding |
|---|
| 25 |
philosophy. It's a practical language for practical programmers |
|---|
| 26 |
who need to get the job done quickly, reliably, and leave behind |
|---|
| 27 |
maintainable, easy to understand code. |
|---|
| 28 |
) |
|---|
| 29 |
|
|---|
| 30 |
$(P D is the culmination of decades of experience implementing |
|---|
| 31 |
compilers for many diverse languages, and attempting to construct |
|---|
| 32 |
large projects using those languages. D draws inspiration from |
|---|
| 33 |
those other languages (most especially C++) and tempers it with |
|---|
| 34 |
experience and real world practicality. |
|---|
| 35 |
) |
|---|
| 36 |
) |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
$(SECTION2 Why D?, |
|---|
| 40 |
|
|---|
| 41 |
$(P Why, indeed. Who needs another programming language? |
|---|
| 42 |
) |
|---|
| 43 |
|
|---|
| 44 |
$(P The software industry has come a long way since the C language was |
|---|
| 45 |
invented. |
|---|
| 46 |
Many new concepts were added to the language with C++, but backwards |
|---|
| 47 |
compatibility with C was maintained, including compatibility with |
|---|
| 48 |
nearly all the weaknesses of the original design. |
|---|
| 49 |
There have been many attempts to fix those weaknesses, but the |
|---|
| 50 |
compatibility issue frustrates it. |
|---|
| 51 |
Meanwhile, both C and C++ undergo a constant accretion of new |
|---|
| 52 |
features. These new features must be carefully fitted into the |
|---|
| 53 |
existing structure without requiring rewriting old code. |
|---|
| 54 |
The end result is very complicated - the C standard is nearly |
|---|
| 55 |
500 pages, and the C++ standard is about 750 pages! |
|---|
| 56 |
C++ is a difficult and costly language to implement, |
|---|
| 57 |
resulting in implementation variations that make it frustrating |
|---|
| 58 |
to write fully portable C++ code. |
|---|
| 59 |
) |
|---|
| 60 |
|
|---|
| 61 |
$(P C++ implements things like resizable arrays and string concatenation |
|---|
| 62 |
as part of the standard library, not as part of the core language. |
|---|
| 63 |
Not being part of the core language has several |
|---|
| 64 |
$(LINK2 cppstrings.html, suboptimal consequences). |
|---|
| 65 |
) |
|---|
| 66 |
|
|---|
| 67 |
$(P Can the power and capability of C++ be extracted, redesigned, |
|---|
| 68 |
and recast into a language that is simple, orthogonal, |
|---|
| 69 |
and practical? |
|---|
| 70 |
Can it all be put into a package |
|---|
| 71 |
that is easy for compiler |
|---|
| 72 |
writers to correctly implement, and |
|---|
| 73 |
which enables compilers to efficiently generate aggressively |
|---|
| 74 |
optimized code? |
|---|
| 75 |
) |
|---|
| 76 |
|
|---|
| 77 |
$(P Modern compiler technology has progressed to the point where language |
|---|
| 78 |
features for the purpose of compensating for primitive compiler |
|---|
| 79 |
technology can be omitted. (An |
|---|
| 80 |
example of this would be the $(SINGLEQUOTE register) keyword in C, a more |
|---|
| 81 |
subtle example is the macro |
|---|
| 82 |
preprocessor in C.) |
|---|
| 83 |
We can rely on modern compiler optimization technology to not |
|---|
| 84 |
need language features necessary to get acceptable code quality out of |
|---|
| 85 |
primitive compilers. |
|---|
| 86 |
) |
|---|
| 87 |
|
|---|
| 88 |
$(SECTION3 Major Design Goals of D, |
|---|
| 89 |
|
|---|
| 90 |
$(P Everything in designing a language is a tradeoff. Keeping some |
|---|
| 91 |
principles in mind will help to make the right decisions.) |
|---|
| 92 |
|
|---|
| 93 |
$(OL |
|---|
| 94 |
$(LI Make it easier to write code that is portable from compiler |
|---|
| 95 |
to compiler, machine to machine, and operating system to operating |
|---|
| 96 |
system. Eliminate undefined and implementation defined behaviors |
|---|
| 97 |
as much as practical.) |
|---|
| 98 |
|
|---|
| 99 |
$(LI Provide syntactic and semantic constructs that eliminate or |
|---|
| 100 |
at least reduce common mistakes. Reduce or even eliminate the need |
|---|
| 101 |
for third party static code checkers.) |
|---|
| 102 |
|
|---|
| 103 |
$(LI Support memory safe programming.) |
|---|
| 104 |
|
|---|
| 105 |
$(LI Support multi-paradigm programming, i.e. at a minimum support |
|---|
| 106 |
imperative, structured, object oriented, generic and even |
|---|
| 107 |
functional programming paradigms.) |
|---|
| 108 |
|
|---|
| 109 |
$(LI Make doing things the right way easier than the wrong way.) |
|---|
| 110 |
|
|---|
| 111 |
$(LI Have a short learning curve for programmers comfortable with |
|---|
| 112 |
programming in C, C++ or Java.) |
|---|
| 113 |
|
|---|
| 114 |
$(LI Provide low level bare metal access as required. Provide a means |
|---|
| 115 |
for the advanced programmer to escape checking as necessary.) |
|---|
| 116 |
|
|---|
| 117 |
$(LI Make D reasonably easy to implement a compiler for.) |
|---|
| 118 |
|
|---|
| 119 |
$(LI Be compatible with the local C application binary interface.) |
|---|
| 120 |
|
|---|
| 121 |
$(LI Where D code looks the same as C code, have it either behave the |
|---|
| 122 |
same or issue an error.) |
|---|
| 123 |
|
|---|
| 124 |
$(LI Have a context-free grammar. Successful parsing must not require |
|---|
| 125 |
semantic analysis.) |
|---|
| 126 |
|
|---|
| 127 |
$(LI Easily support writing internationalized applications.) |
|---|
| 128 |
|
|---|
| 129 |
$(LI Incorporate Contract Programming and unit testing methodology.) |
|---|
| 130 |
|
|---|
| 131 |
$(LI Be able to build lightweight, standalone programs.) |
|---|
| 132 |
|
|---|
| 133 |
$(LI Reduce the costs of creating documentation.) |
|---|
| 134 |
|
|---|
| 135 |
$(LI Provide sufficient semantics to enable advances in compiler optimization |
|---|
| 136 |
technology.) |
|---|
| 137 |
|
|---|
| 138 |
$(LI Cater to the needs of numerical analysis programmers.) |
|---|
| 139 |
|
|---|
| 140 |
$(LI Obviously, sometimes these goals will conflict. Resolution will be |
|---|
| 141 |
in favor of usability.) |
|---|
| 142 |
) |
|---|
| 143 |
) |
|---|
| 144 |
|
|---|
| 145 |
$(SECTION3 Features To Keep, |
|---|
| 146 |
|
|---|
| 147 |
$(P The general look of D is like C and C++. This makes it easier to learn |
|---|
| 148 |
and port code to D. Transitioning from C/C++ to D should feel natural. |
|---|
| 149 |
The |
|---|
| 150 |
programmer will not have to learn an entirely new way of doing things. |
|---|
| 151 |
) |
|---|
| 152 |
|
|---|
| 153 |
$(P Using D will not mean that the programmer will become restricted to a |
|---|
| 154 |
specialized runtime vm (virtual machine) like the Java vm or the |
|---|
| 155 |
Smalltalk vm. |
|---|
| 156 |
There is no D vm, it's a straightforward compiler that generates |
|---|
| 157 |
linkable object files. |
|---|
| 158 |
D connects to the operating system just like C does. |
|---|
| 159 |
The usual familiar tools like $(B make) will fit right in with |
|---|
| 160 |
D development. |
|---|
| 161 |
) |
|---|
| 162 |
|
|---|
| 163 |
$(UL |
|---|
| 164 |
$(LI The general $(B look and feel) of C/C++ is maintained. |
|---|
| 165 |
It uses the same algebraic syntax, most of the same expression |
|---|
| 166 |
and statement forms, and the general layout. |
|---|
| 167 |
) |
|---|
| 168 |
|
|---|
| 169 |
$(LI D programs can be written either in |
|---|
| 170 |
C style $(B function-and-data), |
|---|
| 171 |
C++ style $(B object-oriented), |
|---|
| 172 |
C++ style $(B template metaprogramming), |
|---|
| 173 |
or any mix of the three. |
|---|
| 174 |
) |
|---|
| 175 |
|
|---|
| 176 |
$(LI The $(B compile/link/debug) development model is |
|---|
| 177 |
carried forward, |
|---|
| 178 |
although nothing precludes D from being compiled into bytecode |
|---|
| 179 |
and interpreted. |
|---|
| 180 |
) |
|---|
| 181 |
|
|---|
| 182 |
$(LI $(B Exception handling). |
|---|
| 183 |
More and more experience with exception handling shows it to be a |
|---|
| 184 |
superior way to handle errors than the C traditional method of using |
|---|
| 185 |
error codes and errno globals. |
|---|
| 186 |
) |
|---|
| 187 |
|
|---|
| 188 |
$(LI $(B Runtime Type Identification). |
|---|
| 189 |
This is partially implemented in C++; |
|---|
| 190 |
in D it is taken to its |
|---|
| 191 |
next logical step. Fully supporting it enables better garbage |
|---|
| 192 |
collection, better debugger support, more automated persistence, etc. |
|---|
| 193 |
) |
|---|
| 194 |
|
|---|
| 195 |
$(LI D maintains function link compatibility with the $(B C calling |
|---|
| 196 |
conventions). This makes |
|---|
| 197 |
it possible for D programs to access operating system API's directly. |
|---|
| 198 |
Programmers' knowledge and experience with existing programming API's |
|---|
| 199 |
and paradigms can be carried forward to D with minimal effort. |
|---|
| 200 |
) |
|---|
| 201 |
|
|---|
| 202 |
$(LI $(B Operator overloading). |
|---|
| 203 |
D programs can overload operators enabling |
|---|
| 204 |
extension of the basic types with user defined types. |
|---|
| 205 |
) |
|---|
| 206 |
|
|---|
| 207 |
$(LI $(B Template Metaprogramming). |
|---|
| 208 |
Templates are a way to implement generic programming. |
|---|
| 209 |
Other ways include using macros or having a variant data type. |
|---|
| 210 |
Using macros is out. Variants are straightforward, but |
|---|
| 211 |
inefficient and lack type checking. |
|---|
| 212 |
The difficulties with C++ templates are their |
|---|
| 213 |
complexity, they don't fit well into the syntax of the language, |
|---|
| 214 |
all the various rules for conversions and overloading fitted on top of |
|---|
| 215 |
it, etc. D offers a much simpler way of doing templates. |
|---|
| 216 |
) |
|---|
| 217 |
|
|---|
| 218 |
$(LI <acronym title="Resource Acquisition Is Initialization">$(B RAII)</acronym> |
|---|
| 219 |
(Resource Acquisition Is Initialization). |
|---|
| 220 |
RAII techniques are an essential component of writing reliable |
|---|
| 221 |
software. |
|---|
| 222 |
) |
|---|
| 223 |
|
|---|
| 224 |
$(LI $(B Down and dirty programming). D retains the ability to |
|---|
| 225 |
do down-and-dirty programming without resorting to referring to |
|---|
| 226 |
external modules compiled in a different language. Sometimes, |
|---|
| 227 |
it's just necessary to coerce a pointer or dip into assembly |
|---|
| 228 |
when doing systems work. D's goal is not to $(I prevent) down |
|---|
| 229 |
and dirty programming, but to minimize the need for it in |
|---|
| 230 |
solving routine coding tasks. |
|---|
| 231 |
) |
|---|
| 232 |
) |
|---|
| 233 |
) |
|---|
| 234 |
|
|---|
| 235 |
$(SECTION3 Features To Drop, |
|---|
| 236 |
|
|---|
| 237 |
$(UL |
|---|
| 238 |
$(LI C source code compatibility. Extensions to C that maintain |
|---|
| 239 |
source compatibility |
|---|
| 240 |
have already been done (C++ and ObjectiveC). Further work in this |
|---|
| 241 |
area is hampered by so much legacy code it is unlikely that significant |
|---|
| 242 |
improvements can be made. |
|---|
| 243 |
) |
|---|
| 244 |
|
|---|
| 245 |
$(LI Link compatibility with C++. The C++ runtime object model is just |
|---|
| 246 |
too complicated - properly supporting it would essentially imply |
|---|
| 247 |
making D a full C++ compiler too. |
|---|
| 248 |
) |
|---|
| 249 |
|
|---|
| 250 |
$(LI The C preprocessor. Macro processing is an easy way to extend |
|---|
| 251 |
a language, adding in faux features that aren't really there (invisible |
|---|
| 252 |
to the symbolic debugger). Conditional compilation, layered with |
|---|
| 253 |
#include text, macros, token concatenation, etc., essentially forms |
|---|
| 254 |
not one language but two merged together with no obvious distinction |
|---|
| 255 |
between them. Even worse (or perhaps for the best) the C preprocessor |
|---|
| 256 |
is a very primitive macro language. It's time to step back, look at |
|---|
| 257 |
what the preprocessor is used for, and design support for those |
|---|
| 258 |
capabilities directly into the language. |
|---|
| 259 |
) |
|---|
| 260 |
|
|---|
| 261 |
$(LI Multiple inheritance. It's a complex |
|---|
| 262 |
feature of debatable value. It's very difficult to implement in an |
|---|
| 263 |
efficient manner, and compilers are prone to many bugs in implementing |
|---|
| 264 |
it. Nearly all the value of |
|---|
| 265 |
<acronym title="multiple inheritance">MI</acronym> can be handled with |
|---|
| 266 |
single inheritance |
|---|
| 267 |
coupled with interfaces and aggregation. What's left does not |
|---|
| 268 |
justify the weight of MI implementation. |
|---|
| 269 |
) |
|---|
| 270 |
|
|---|
| 271 |
$(LI Namespaces. An attempt to deal with the problems resulting from |
|---|
| 272 |
linking together independently developed pieces of code that |
|---|
| 273 |
have conflicting names. The idea of modules is simpler and works |
|---|
| 274 |
much better. |
|---|
| 275 |
) |
|---|
| 276 |
|
|---|
| 277 |
$(LI Tag name space. This misfeature of C is where the tag names |
|---|
| 278 |
of structs are in a separate but parallel symbol table. C++ |
|---|
| 279 |
attempted to merge the tag name space with the regular name space, |
|---|
| 280 |
while retaining backward compatibility with legacy C code. The |
|---|
| 281 |
result is needlessly confusing. |
|---|
| 282 |
) |
|---|
| 283 |
|
|---|
| 284 |
$(LI Forward declarations. C compilers semantically only know |
|---|
| 285 |
about what has lexically preceded the current state. C++ extends this |
|---|
| 286 |
a little, in that class members can rely on forward referenced class |
|---|
| 287 |
members. D takes this to its logical conclusion, forward declarations |
|---|
| 288 |
are no longer necessary at the module level. |
|---|
| 289 |
Functions can be defined in a natural |
|---|
| 290 |
order rather than the typical inside-out order commonly used in C |
|---|
| 291 |
programs to avoid writing forward declarations. |
|---|
| 292 |
) |
|---|
| 293 |
|
|---|
| 294 |
$(LI Include files. A major cause of slow compiles as each |
|---|
| 295 |
compilation unit |
|---|
| 296 |
must reparse enormous quantities of header files. Include files |
|---|
| 297 |
should be done as importing a symbol table. |
|---|
| 298 |
) |
|---|
| 299 |
|
|---|
| 300 |
$(LI Trigraphs and digraphs. Unicode is the future. |
|---|
| 301 |
) |
|---|
| 302 |
|
|---|
| 303 |
$(LI Non-virtual member functions. In C++, a class designer decides |
|---|
| 304 |
in advance if a function is to be virtual or not. Forgetting to retrofit |
|---|
| 305 |
the base class member function to be virtual when the function gets |
|---|
| 306 |
overridden is a common (and very hard to find) coding error. |
|---|
| 307 |
Making all member functions virtual, and letting the compiler decide |
|---|
| 308 |
if there are no overrides and hence can be converted to non-virtual, |
|---|
| 309 |
is much more reliable. |
|---|
| 310 |
) |
|---|
| 311 |
|
|---|
| 312 |
$(LI Bit fields of arbitrary size. |
|---|
| 313 |
Bit fields are a complex, inefficient feature rarely used. |
|---|
| 314 |
) |
|---|
| 315 |
|
|---|
| 316 |
$(LI Support for 16 bit computers. |
|---|
| 317 |
No consideration is given in D for mixed near/far pointers and all the |
|---|
| 318 |
machinations necessary to generate good 16 bit code. The D language |
|---|
| 319 |
design assumes at least a 32 bit flat memory space. D will fit smoothly |
|---|
| 320 |
into 64 bit architectures. |
|---|
| 321 |
) |
|---|
| 322 |
|
|---|
| 323 |
$(LI Mutual dependence of compiler passes. In C++, successfully parsing |
|---|
| 324 |
the source text relies on having a symbol table, and on the various |
|---|
| 325 |
preprocessor commands. This makes it |
|---|
| 326 |
impossible to preparse C++ source, and makes writing code analyzers |
|---|
| 327 |
and syntax directed editors painfully difficult to do correctly. |
|---|
| 328 |
) |
|---|
| 329 |
|
|---|
| 330 |
$(LI Compiler complexity. Reducing the complexity of an implementation |
|---|
| 331 |
makes it more likely that multiple, $(I correct) implementations |
|---|
| 332 |
are available. |
|---|
| 333 |
) |
|---|
| 334 |
|
|---|
| 335 |
$(LI Dumbed down floating point. If one is using hardware that |
|---|
| 336 |
implements modern floating point, it should be available to the |
|---|
| 337 |
programmer rather than having floating point support dumbed down |
|---|
| 338 |
to the lowest common denominator among machines. In particular, |
|---|
| 339 |
a D implementation must support IEEE 754 arithmetic and if |
|---|
| 340 |
extended precision is available it must be supported.) |
|---|
| 341 |
|
|---|
| 342 |
$(LI Template overloading of < and > symbols. |
|---|
| 343 |
This choice has caused years of bugs, grief, and confusion |
|---|
| 344 |
for programmers, C++ implementors, and C++ source parsing tool |
|---|
| 345 |
vendors. It makes it |
|---|
| 346 |
impossible to parse C++ code correctly without doing a nearly complete |
|---|
| 347 |
C++ compiler. D uses !( and ) which fit neatly and |
|---|
| 348 |
unambiguously into the grammar. |
|---|
| 349 |
) |
|---|
| 350 |
) |
|---|
| 351 |
) |
|---|
| 352 |
|
|---|
| 353 |
$(SECTION3 Who D is For, |
|---|
| 354 |
|
|---|
| 355 |
$(UL |
|---|
| 356 |
$(LI Programmers who routinely use lint or similar code analysis tools |
|---|
| 357 |
to eliminate bugs before the code is even compiled. |
|---|
| 358 |
) |
|---|
| 359 |
|
|---|
| 360 |
$(LI People who compile with maximum warning levels turned on and who |
|---|
| 361 |
instruct the compiler to treat warnings as errors. |
|---|
| 362 |
) |
|---|
| 363 |
|
|---|
| 364 |
$(LI Programming managers who are forced to rely on programming style |
|---|
| 365 |
guidelines to avoid common C bugs. |
|---|
| 366 |
) |
|---|
| 367 |
|
|---|
| 368 |
$(LI Those who decide the promise of C++ object oriented |
|---|
| 369 |
programming is not fulfilled due to the complexity of it. |
|---|
| 370 |
) |
|---|
| 371 |
|
|---|
| 372 |
$(LI Programmers who enjoy the expressive power of C++ but are |
|---|
| 373 |
frustrated by |
|---|
| 374 |
the need to expend much effort explicitly managing memory and finding |
|---|
| 375 |
pointer bugs. |
|---|
| 376 |
) |
|---|
| 377 |
|
|---|
| 378 |
$(LI Projects that need built-in testing and verification. |
|---|
| 379 |
) |
|---|
| 380 |
|
|---|
| 381 |
$(LI Teams who write apps with a million lines of code in it. |
|---|
| 382 |
) |
|---|
| 383 |
|
|---|
| 384 |
$(LI Programmers who think the language should provide enough |
|---|
| 385 |
features to obviate |
|---|
| 386 |
the continual necessity to manipulate pointers directly. |
|---|
| 387 |
) |
|---|
| 388 |
|
|---|
| 389 |
$(LI Numerical programmers. D has many features to directly |
|---|
| 390 |
support features needed by numerics programmers, like |
|---|
| 391 |
extended floating point precision, |
|---|
| 392 |
core support for complex and imaginary floating types |
|---|
| 393 |
and defined behavior for |
|---|
| 394 |
<acronym title="Not A Number">NaN</acronym>'s and infinities. |
|---|
| 395 |
(These are added in the new |
|---|
| 396 |
C99 standard, but not in C++.) |
|---|
| 397 |
) |
|---|
| 398 |
|
|---|
| 399 |
$(LI Programmers who write half their application in scripting |
|---|
| 400 |
langauges like Ruby and Python, and the other half in C++ to |
|---|
| 401 |
speed up the bottlenecks. D has many of the productivity features |
|---|
| 402 |
of Ruby and Python, making it possible to write the entire app |
|---|
| 403 |
in one language.) |
|---|
| 404 |
|
|---|
| 405 |
$(LI D's lexical analyzer and parser are totally independent of each other and of the |
|---|
| 406 |
semantic analyzer. This means it is easy to write simple tools to manipulate D source |
|---|
| 407 |
perfectly without having to build a full compiler. It also means that source code can be |
|---|
| 408 |
transmitted in tokenized form for specialized applications. |
|---|
| 409 |
) |
|---|
| 410 |
) |
|---|
| 411 |
) |
|---|
| 412 |
|
|---|
| 413 |
$(SECTION3 Who D is Not For, |
|---|
| 414 |
|
|---|
| 415 |
$(UL |
|---|
| 416 |
$(LI Realistically, nobody is going to convert million line C or C++ |
|---|
| 417 |
programs into D. |
|---|
| 418 |
Since D does not compile unmodified C/C++ |
|---|
| 419 |
source code, D is not for |
|---|
| 420 |
legacy apps. |
|---|
| 421 |
(However, D supports legacy C API's very well. D can connect |
|---|
| 422 |
directly to any code that exposes a C interface.) |
|---|
| 423 |
) |
|---|
| 424 |
|
|---|
| 425 |
$(LI As a first programming language - Basic or Java is more suitable |
|---|
| 426 |
for beginners. D makes an excellent second language for intermediate |
|---|
| 427 |
to advanced programmers. |
|---|
| 428 |
) |
|---|
| 429 |
|
|---|
| 430 |
$(LI Language purists. D is a practical language, and each feature |
|---|
| 431 |
of it is evaluated in that light, rather than by an ideal. |
|---|
| 432 |
For example, D has constructs and semantics that virtually eliminate |
|---|
| 433 |
the need for pointers for ordinary tasks. But pointers are still |
|---|
| 434 |
there, because sometimes the rules need to be broken. |
|---|
| 435 |
Similarly, casts are still there for those times when the typing |
|---|
| 436 |
system needs to be overridden. |
|---|
| 437 |
) |
|---|
| 438 |
) |
|---|
| 439 |
) |
|---|
| 440 |
|
|---|
| 441 |
) |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
$(SECTION2 Major Features of D, |
|---|
| 446 |
|
|---|
| 447 |
$(P This section lists some of the more interesting features of D |
|---|
| 448 |
in various categories. |
|---|
| 449 |
) |
|---|
| 450 |
|
|---|
| 451 |
$(SECTION3 Object Oriented Programming, |
|---|
| 452 |
|
|---|
| 453 |
$(SECTION4 Classes, |
|---|
| 454 |
|
|---|
| 455 |
$(P D's object oriented nature comes from classes. |
|---|
| 456 |
The inheritance model is single inheritance enhanced |
|---|
| 457 |
with interfaces. The class Object sits at the root |
|---|
| 458 |
of the inheritance hierarchy, so all classes implement |
|---|
| 459 |
a common set of functionality. |
|---|
| 460 |
Classes are instantiated |
|---|
| 461 |
by reference, and so complex code to clean up after exceptions |
|---|
| 462 |
is not required. |
|---|
| 463 |
) |
|---|
| 464 |
) |
|---|
| 465 |
|
|---|
| 466 |
$(SECTION4 Operator Overloading, |
|---|
| 467 |
|
|---|
| 468 |
$(P Classes can be crafted that work with existing operators to extend |
|---|
| 469 |
the type system to support new types. An example would be creating |
|---|
| 470 |
a bignumber class and then overloading the +, -, * and / operators |
|---|
| 471 |
to enable using ordinary algebraic syntax with them. |
|---|
| 472 |
) |
|---|
| 473 |
) |
|---|
| 474 |
) |
|---|
| 475 |
|
|---|
| 476 |
$(SECTION3 Functional Programming, |
|---|
| 477 |
|
|---|
| 478 |
$(P Functional programming has a lot to offer in terms of encapsulation, |
|---|
| 479 |
concurrent programming, memory safety, and composition. D's support for |
|---|
| 480 |
functional style programming include: |
|---|
| 481 |
) |
|---|
| 482 |
|
|---|
| 483 |
$(UL |
|---|
| 484 |
$(LI Pure functions) |
|---|
| 485 |
$(LI Immutable types and data structures) |
|---|
| 486 |
$(LI Lambda functions and closures) |
|---|
| 487 |
) |
|---|
| 488 |
|
|---|
| 489 |
) |
|---|
| 490 |
|
|---|
| 491 |
$(SECTION3 Productivity, |
|---|
| 492 |
|
|---|
| 493 |
$(SECTION4 Modules, |
|---|
| 494 |
|
|---|
| 495 |
$(P Source files have a one-to-one correspondence with modules. |
|---|
| 496 |
Instead of #include'ing the text of a file of declarations, |
|---|
| 497 |
just import the module. There is no need to worry about |
|---|
| 498 |
multiple imports of the same module, no need to wrapper header |
|---|
| 499 |
files with $(TT #ifndef/#endif) or $(TT #pragma once) kludges, |
|---|
| 500 |
etc. |
|---|
| 501 |
) |
|---|
| 502 |
) |
|---|
| 503 |
|
|---|
| 504 |
$(SECTION4 Declaration vs Definition, |
|---|
| 505 |
|
|---|
| 506 |
$(P C++ usually requires that functions and classes be declared twice - the declaration |
|---|
| 507 |
that goes in the .h header file, and the definition that goes in the .c source |
|---|
| 508 |
file. This is an error prone and tedious process. Obviously, the programmer |
|---|
| 509 |
should only need to write it once, and the compiler should then extract the |
|---|
| 510 |
declaration information and make it available for symbolic importing. This is |
|---|
| 511 |
exactly how D works. |
|---|
| 512 |
) |
|---|
| 513 |
|
|---|
| 514 |
$(P Example: |
|---|
| 515 |
) |
|---|
| 516 |
|
|---|
| 517 |
----------------------- |
|---|
| 518 |
class ABC |
|---|
| 519 |
{ |
|---|
| 520 |
int func() { return 7; } |
|---|
| 521 |
static int z = 7; |
|---|
| 522 |
} |
|---|
| 523 |
int q; |
|---|
| 524 |
----------------------- |
|---|
| 525 |
|
|---|
| 526 |
$(P There is no longer a need for a separate definition of member functions, static |
|---|
| 527 |
members, externs, nor for clumsy syntaxes like: |
|---|
| 528 |
) |
|---|
| 529 |
|
|---|
| 530 |
$(CCODE |
|---|
| 531 |
int ABC::func() { return 7; } |
|---|
| 532 |
int ABC::z = 7; |
|---|
| 533 |
extern int q; |
|---|
| 534 |
) |
|---|
| 535 |
|
|---|
| 536 |
$(P Note: Of course, in C++, trivial functions like $(TT { return 7; }) |
|---|
| 537 |
are written inline too, but complex ones are not. In addition, if |
|---|
| 538 |
there are any forward references, the functions need to be prototyped. |
|---|
| 539 |
The following will not work in C++: |
|---|
| 540 |
) |
|---|
| 541 |
|
|---|
| 542 |
$(CCODE |
|---|
| 543 |
class Foo |
|---|
| 544 |
{ |
|---|
| 545 |
int foo(Bar *c) { return c->bar(); } |
|---|
| 546 |
}; |
|---|
| 547 |
|
|---|
| 548 |
class Bar |
|---|
| 549 |
{ |
|---|
| 550 |
public: |
|---|
| 551 |
int bar() { return 3; } |
|---|
| 552 |
}; |
|---|
| 553 |
) |
|---|
| 554 |
|
|---|
| 555 |
$(P But the equivalent D code will work: |
|---|
| 556 |
) |
|---|
| 557 |
|
|---|
| 558 |
----------------------- |
|---|
| 559 |
class Foo |
|---|
| 560 |
{ |
|---|
| 561 |
int foo(Bar c) { return c.bar; } |
|---|
| 562 |
} |
|---|
| 563 |
|
|---|
| 564 |
class Bar |
|---|
| 565 |
{ |
|---|
| 566 |
int bar() { return 3; } |
|---|
| 567 |
} |
|---|
| 568 |
----------------------- |
|---|
| 569 |
|
|---|
| 570 |
$(P Whether a D function is inlined or not is determined by the |
|---|
| 571 |
optimizer settings. |
|---|
| 572 |
) |
|---|
| 573 |
) |
|---|
| 574 |
|
|---|
| 575 |
$(SECTION4 Templates, |
|---|
| 576 |
|
|---|
| 577 |
$(P D templates offer a clean way to support generic programming while |
|---|
| 578 |
offering the power of partial specialization. |
|---|
| 579 |
Template classes and template functions are available, along |
|---|
| 580 |
with variadic template arguments and tuples. |
|---|
| 581 |
) |
|---|
| 582 |
) |
|---|
| 583 |
|
|---|
| 584 |
$(SECTION4 Associative Arrays, |
|---|
| 585 |
|
|---|
| 586 |
$(P Associative arrays are arrays with an arbitrary data type as |
|---|
| 587 |
the index rather than being limited to an integer index. |
|---|
| 588 |
In essence, associated arrays are hash tables. Associative |
|---|
| 589 |
arrays make it easy to build fast, efficient, bug-free symbol |
|---|
| 590 |
tables. |
|---|
| 591 |
) |
|---|
| 592 |
) |
|---|
| 593 |
|
|---|
| 594 |
$(V1 |
|---|
| 595 |
$(SECTION4 Real Typedefs, |
|---|
| 596 |
|
|---|
| 597 |
$(P C and C++ typedefs are really type $(I aliases), as no new |
|---|
| 598 |
type is really introduced. D implements real typedefs, where: |
|---|
| 599 |
) |
|---|
| 600 |
|
|---|
| 601 |
----------------------- |
|---|
| 602 |
typedef int handle; |
|---|
| 603 |
----------------------- |
|---|
| 604 |
|
|---|
| 605 |
$(P really does create a new type $(B handle). Type checking is |
|---|
| 606 |
enforced, and typedefs participate in function overloading. |
|---|
| 607 |
For example: |
|---|
| 608 |
) |
|---|
| 609 |
|
|---|
| 610 |
----------------------- |
|---|
| 611 |
int foo(int i); |
|---|
| 612 |
int foo(handle h); |
|---|
| 613 |
----------------------- |
|---|
| 614 |
) |
|---|
| 615 |
) |
|---|
| 616 |
|
|---|
| 617 |
$(SECTION4 Documentation, |
|---|
| 618 |
|
|---|
| 619 |
$(P Documentation has traditionally been done twice - first there |
|---|
| 620 |
are comments documenting what a function does, and then this gets |
|---|
| 621 |
rewritten into a separate html or man page. |
|---|
| 622 |
And naturally, over time, they'll tend to diverge as the code |
|---|
| 623 |
gets updated and the separate documentation doesn't. |
|---|
| 624 |
Being able to generate the requisite polished documentation directly |
|---|
| 625 |
from the comments embedded in the source will not only cut the time |
|---|
| 626 |
in half needed to prepare documentation, it will make it much easier |
|---|
| 627 |
to keep the documentation in sync with the code. |
|---|
| 628 |
$(LINK2 ddoc.html, Ddoc) is the specification for the D |
|---|
| 629 |
documentation generator. This page was generated by Ddoc, too. |
|---|
| 630 |
) |
|---|
| 631 |
|
|---|
| 632 |
$(P Although third party tools exist to do this for C++, they have some |
|---|
| 633 |
serious shortcomings: |
|---|
| 634 |
|
|---|
| 635 |
$(UL |
|---|
| 636 |
|
|---|
| 637 |
$(LI It is spectacularly difficult to parse C++ 100% correctly. To |
|---|
| 638 |
do so really requires a full C++ compiler. Third party tools tend to |
|---|
| 639 |
parse only a subset of C++ correctly, so their use will constrain |
|---|
| 640 |
the source code to that subset.) |
|---|
| 641 |
|
|---|
| 642 |
$(LI Different compilers support different versions of C++ and have |
|---|
| 643 |
different extensions to C++. Third party tools have a problem matching |
|---|
| 644 |
all these variations.) |
|---|
| 645 |
|
|---|
| 646 |
$(LI Third party tools may not be available for all the desired |
|---|
| 647 |
platforms, and they're necessarily on a different upgrade cycle |
|---|
| 648 |
from the compilers.) |
|---|
| 649 |
|
|---|
| 650 |
$(LI Having it builtin to the compiler means it is standardized across |
|---|
| 651 |
all D implementations. Having a default one ready to go at all times |
|---|
| 652 |
means it is far more likely to be used.) |
|---|
| 653 |
|
|---|
| 654 |
) |
|---|
| 655 |
) |
|---|
| 656 |
) |
|---|
| 657 |
) |
|---|
| 658 |
|
|---|
| 659 |
$(SECTION3 Functions, |
|---|
| 660 |
|
|---|
| 661 |
$(P D has the expected support for ordinary functions including |
|---|
| 662 |
global functions, overloaded functions, inlining of functions, |
|---|
| 663 |
member functions, virtual functions, function pointers, etc. |
|---|
| 664 |
In addition: |
|---|
| 665 |
) |
|---|
| 666 |
|
|---|
| 667 |
$(SECTION4 Nested Functions, |
|---|
| 668 |
|
|---|
| 669 |
$(P Functions can be nested within other functions. |
|---|
| 670 |
This is highly useful for code factoring, locality, and |
|---|
| 671 |
function closure techniques. |
|---|
| 672 |
) |
|---|
| 673 |
) |
|---|
| 674 |
|
|---|
| 675 |
$(SECTION4 Function Literals, |
|---|
| 676 |
|
|---|
| 677 |
$(P Anonymous functions can be embedded directly into an expression. |
|---|
| 678 |
) |
|---|
| 679 |
) |
|---|
| 680 |
|
|---|
| 681 |
$(SECTION4 Dynamic Closures, |
|---|
| 682 |
|
|---|
| 683 |
$(P Nested functions and class member functions can be referenced |
|---|
| 684 |
with closures (also called delegates), making generic programming |
|---|
| 685 |
much easier and type safe. |
|---|
| 686 |
) |
|---|
| 687 |
) |
|---|
| 688 |
|
|---|
| 689 |
$(SECTION4 In Out and Inout Parameters, |
|---|
| 690 |
|
|---|
| 691 |
$(P Not only does specifying this help make functions more |
|---|
| 692 |
self-documenting, it eliminates much of the necessity for pointers |
|---|
| 693 |
without sacrificing anything, and it opens up possibilities |
|---|
| 694 |
for more compiler help in finding coding problems. |
|---|
| 695 |
) |
|---|
| 696 |
|
|---|
| 697 |
$(P Such makes it possible for D to directly interface to a |
|---|
| 698 |
wider variety of foreign API's. There would be no need for |
|---|
| 699 |
workarounds like "Interface Definition Languages". |
|---|
| 700 |
) |
|---|
| 701 |
) |
|---|
| 702 |
) |
|---|
| 703 |
|
|---|
| 704 |
$(SECTION3 Arrays, |
|---|
| 705 |
|
|---|
| 706 |
$(P C arrays have several faults that can be corrected: |
|---|
| 707 |
) |
|---|
| 708 |
|
|---|
| 709 |
$(UL |
|---|
| 710 |
|
|---|
| 711 |
$(LI Dimension information is not carried around with |
|---|
| 712 |
the array, and so has to be stored and passed separately. |
|---|
| 713 |
The classic example of this are the argc and argv |
|---|
| 714 |
parameters to $(TT main(int $(D_PARAM argc), char *$(D_PARAM argv)[])). |
|---|
| 715 |
(In D, main is declared as $(TT main(char[][] $(D_PARAM args))).) |
|---|
| 716 |
) |
|---|
| 717 |
|
|---|
| 718 |
$(LI Arrays are not first class objects. When an array is passed to a function, it is |
|---|
| 719 |
converted to a pointer, even though the prototype confusingly says it's an |
|---|
| 720 |
array. When this conversion happens, all array type information |
|---|
| 721 |
gets lost. |
|---|
| 722 |
) |
|---|
| 723 |
|
|---|
| 724 |
$(LI C arrays cannot be resized. This means that even simple aggregates like a stack |
|---|
| 725 |
need to be constructed as a complex class.) |
|---|
| 726 |
|
|---|
| 727 |
$(LI C arrays cannot be bounds checked, because they don't know |
|---|
| 728 |
what the array bounds are.) |
|---|
| 729 |
|
|---|
| 730 |
$(LI Arrays are declared with the [] after the identifier. This leads to |
|---|
| 731 |
very clumsy |
|---|
| 732 |
syntax to declare things like a pointer to an array: |
|---|
| 733 |
|
|---|
| 734 |
$(CCODE |
|---|
| 735 |
int (*array)[3]; |
|---|
| 736 |
) |
|---|
| 737 |
|
|---|
| 738 |
$(P In D, the [] for the array go on the left: |
|---|
| 739 |
) |
|---|
| 740 |
|
|---|
| 741 |
----------------------- |
|---|
| 742 |
int[3]* array; // declares a pointer to an array of 3 ints |
|---|
| 743 |
long[] func(int x); // declares a function returning an array of longs |
|---|
| 744 |
----------------------- |
|---|
| 745 |
|
|---|
| 746 |
$(P which is much simpler to understand. |
|---|
| 747 |
) |
|---|
| 748 |
) |
|---|
| 749 |
) |
|---|
| 750 |
|
|---|
| 751 |
$(P D arrays come in several varieties: pointers, static arrays, dynamic |
|---|
| 752 |
arrays, and associative arrays. |
|---|
| 753 |
) |
|---|
| 754 |
|
|---|
| 755 |
$(P See $(LINK2 arrays.html, Arrays). |
|---|
| 756 |
) |
|---|
| 757 |
|
|---|
| 758 |
$(SECTION4 Strings, |
|---|
| 759 |
|
|---|
| 760 |
$(P String manipulation is so common, and so clumsy in C and C++, that |
|---|
| 761 |
it needs direct support in the language. Modern languages handle |
|---|
| 762 |
string concatenation, copying, etc., and so does D. Strings are |
|---|
| 763 |
a direct consequence of improved array handling. |
|---|
| 764 |
) |
|---|
| 765 |
) |
|---|
| 766 |
) |
|---|
| 767 |
|
|---|
| 768 |
$(SECTION3 Resource Management, |
|---|
| 769 |
|
|---|
| 770 |
$(SECTION4 Automatic Memory Management, |
|---|
| 771 |
|
|---|
| 772 |
$(P D memory allocation is fully garbage collected. Empirical experience |
|---|
| 773 |
suggests that a lot of the complicated features of C++ are necessary |
|---|
| 774 |
in order to manage memory deallocation. With garbage collection, the |
|---|
| 775 |
language gets much simpler. |
|---|
| 776 |
) |
|---|
| 777 |
|
|---|
| 778 |
$(P There's a perception that garbage collection is for lazy, junior |
|---|
| 779 |
programmers. I remember when that was said about C++, after all, |
|---|
| 780 |
there's nothing in C++ that cannot be done in C, or in assembler |
|---|
| 781 |
for that matter. |
|---|
| 782 |
) |
|---|
| 783 |
|
|---|
| 784 |
$(P Garbage collection eliminates the tedious, error prone memory |
|---|
| 785 |
allocation |
|---|
| 786 |
tracking code necessary in C and C++. This not only means much |
|---|
| 787 |
faster development time and lower maintenance costs, |
|---|
| 788 |
but the resulting program frequently runs |
|---|
| 789 |
faster! |
|---|
| 790 |
) |
|---|
| 791 |
|
|---|
| 792 |
$(P Sure, garbage collectors can be used with C++, and I've used them |
|---|
| 793 |
in my own C++ projects. The language isn't friendly to collectors, |
|---|
| 794 |
however, impeding the effectiveness of it. Much of the runtime |
|---|
| 795 |
library code can't be used with |
|---|
| 796 |
collectors. |
|---|
| 797 |
) |
|---|
| 798 |
|
|---|
| 799 |
$(P For a fuller discussion of this, see |
|---|
| 800 |
$(LINK2 garbage.html, garbage collection). |
|---|
| 801 |
) |
|---|
| 802 |
) |
|---|
| 803 |
|
|---|
| 804 |
$(SECTION4 Explicit Memory Management, |
|---|
| 805 |
|
|---|
| 806 |
$(P Despite D being a garbage collected language, the new and delete |
|---|
| 807 |
operations can be overridden for particular classes so that |
|---|
| 808 |
a custom allocator can be used. |
|---|
| 809 |
) |
|---|
| 810 |
) |
|---|
| 811 |
|
|---|
| 812 |
$(SECTION4 RAII, |
|---|
| 813 |
|
|---|
| 814 |
$(P RAII is a modern software development technique to manage resource |
|---|
| 815 |
allocation and deallocation. D supports RAII in a controlled, |
|---|
| 816 |
predictable manner that is independent of the garbage collection |
|---|
| 817 |
cycle. |
|---|
| 818 |
) |
|---|
| 819 |
) |
|---|
| 820 |
) |
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
$(SECTION3 Performance, |
|---|
| 824 |
|
|---|
| 825 |
$(SECTION4 Lightweight Aggregates, |
|---|
| 826 |
|
|---|
| 827 |
$(P D supports simple C style structs, both for compatibility with |
|---|
| 828 |
C data structures and because they're useful when the full power |
|---|
| 829 |
of classes is overkill. |
|---|
| 830 |
) |
|---|
| 831 |
) |
|---|
| 832 |
|
|---|
| 833 |
$(SECTION4 Inline Assembler, |
|---|
| 834 |
|
|---|
| 835 |
$(P Device drivers, high performance system applications, embedded systems, |
|---|
| 836 |
and specialized code sometimes need to dip into assembly language |
|---|
| 837 |
to get the job done. While D implementations are not required |
|---|
| 838 |
to implement the inline assembler, it is defined and part of the |
|---|
| 839 |
language. Most assembly code needs can be handled with it, |
|---|
| 840 |
obviating the need for separate assemblers or DLL's. |
|---|
| 841 |
) |
|---|
| 842 |
|
|---|
| 843 |
$(P Many D implementations will also support intrinsic functions |
|---|
| 844 |
analogously to C's support of intrinsics for I/O port manipulation, |
|---|
| 845 |
direct access to special floating point operations, etc. |
|---|
| 846 |
) |
|---|
| 847 |
) |
|---|
| 848 |
) |
|---|
| 849 |
|
|---|
| 850 |
|
|---|
| 851 |
$(SECTION3 Reliability, |
|---|
| 852 |
|
|---|
| 853 |
$(P A modern language should do all it can to help the programmer flush |
|---|
| 854 |
out bugs in the code. Help can come in many forms; |
|---|
| 855 |
from making it easy to use more robust techniques, |
|---|
| 856 |
to compiler flagging of obviously incorrect code, to runtime checking. |
|---|
| 857 |
) |
|---|
| 858 |
|
|---|
| 859 |
$(SECTION4 Contracts, |
|---|
| 860 |
|
|---|
| 861 |
$(P Contract Programming (invented by B. Meyer) is a revolutionary |
|---|
| 862 |
technique |
|---|
| 863 |
to aid in ensuring the correctness of programs. D's version of |
|---|
| 864 |
DBC includes function preconditions, function postconditions, class |
|---|
| 865 |
invariants, and assert contracts. |
|---|
| 866 |
See $(LINK2 dbc.html, Contracts) for D's implementation. |
|---|
| 867 |
) |
|---|
| 868 |
) |
|---|
| 869 |
|
|---|
| 870 |
$(SECTION4 Unit Tests, |
|---|
| 871 |
|
|---|
| 872 |
$(P Unit tests can be added to a class, such that they are automatically |
|---|
| 873 |
run upon program startup. This aids in verifying, in every build, |
|---|
| 874 |
that class implementations weren't inadvertently broken. The unit |
|---|
| 875 |
tests form part of the source code for a class. Creating them |
|---|
| 876 |
becomes a natural part of the class development process, as opposed |
|---|
| 877 |
to throwing the finished code over the wall to the testing group. |
|---|
| 878 |
) |
|---|
| 879 |
|
|---|
| 880 |
$(P Unit tests can be done in other languages, but the result is kludgy |
|---|
| 881 |
and the languages just aren't accommodating of the concept. |
|---|
| 882 |
Unit testing is a main feature of D. For library functions it works |
|---|
| 883 |
out great, serving both to guarantee that the functions |
|---|
| 884 |
actually work and to illustrate how to use the functions. |
|---|
| 885 |
) |
|---|
| 886 |
|
|---|
| 887 |
$(P Consider the many C++ library and application code bases out there for |
|---|
| 888 |
download on the web. How much of it comes with *any* verification |
|---|
| 889 |
tests at all, let alone unit testing? Less than 1%? The usual practice |
|---|
| 890 |
is if it compiles, we assume it works. And we wonder if the warnings |
|---|
| 891 |
the compiler spits out in the process are real bugs or just nattering |
|---|
| 892 |
about nits. |
|---|
| 893 |
) |
|---|
| 894 |
|
|---|
| 895 |
$(P Along with Contract Programming, unit testing makes D far and away |
|---|
| 896 |
the best language for writing reliable, robust systems applications. |
|---|
| 897 |
Unit testing also gives us a quick-and-dirty estimate of the quality |
|---|
| 898 |
of some unknown piece of D code dropped in our laps - if it has no |
|---|
| 899 |
unit tests and no contracts, it's unacceptable. |
|---|
| 900 |
) |
|---|
| 901 |
) |
|---|
| 902 |
|
|---|
| 903 |
|
|---|
| 904 |
$(SECTION4 Debug Attributes and Statements, |
|---|
| 905 |
|
|---|
| 906 |
$(P Now debug is part of the syntax of the language. |
|---|
| 907 |
The code can be enabled or disabled at compile time, without the |
|---|
| 908 |
use of macros or preprocessing commands. The debug syntax enables |
|---|
| 909 |
a consistent, portable, and understandable recognition that real |
|---|
| 910 |
source code needs to be able to generate both debug compilations and |
|---|
| 911 |
release compilations. |
|---|
| 912 |
) |
|---|
| 913 |
) |
|---|
| 914 |
|
|---|
| 915 |
$(SECTION4 Exception Handling, |
|---|
| 916 |
|
|---|
| 917 |
$(P The superior $(I try-catch-finally) model is used rather than just |
|---|
| 918 |
try-catch. There's no need to create dummy objects just to have |
|---|
| 919 |
the destructor implement the $(I finally) semantics. |
|---|
| 920 |
) |
|---|
| 921 |
) |
|---|
| 922 |
|
|---|
| 923 |
$(SECTION4 Synchronization, |
|---|
| 924 |
|
|---|
| 925 |
$(P Multithreaded programming is becoming more and more mainstream, |
|---|
| 926 |
and D provides primitives to build multithreaded programs with. |
|---|
| 927 |
Synchronization can be done at either the method or the object level. |
|---|
| 928 |
) |
|---|
| 929 |
|
|---|
| 930 |
----------------------- |
|---|
| 931 |
synchronized int func() { ... } |
|---|
| 932 |
----------------------- |
|---|
| 933 |
|
|---|
| 934 |
$(P Synchronized functions allow only one thread at a time to be |
|---|
| 935 |
executing that function. |
|---|
| 936 |
) |
|---|
| 937 |
|
|---|
| 938 |
$(P The synchronize statement puts a mutex around a block of statements, |
|---|
| 939 |
controlling access either by object or globally. |
|---|
| 940 |
) |
|---|
| 941 |
) |
|---|
| 942 |
|
|---|
| 943 |
$(SECTION4 Support for Robust Techniques, |
|---|
| 944 |
|
|---|
| 945 |
$(UL |
|---|
| 946 |
$(LI Dynamic arrays instead of pointers) |
|---|
| 947 |
|
|---|
| 948 |
$(LI Reference variables instead of pointers) |
|---|
| 949 |
|
|---|
| 950 |
$(LI Reference objects instead of pointers) |
|---|
| 951 |
|
|---|
| 952 |
$(LI Garbage collection instead of explicit memory management) |
|---|
| 953 |
|
|---|
| 954 |
$(LI Built-in primitives for thread synchronization) |
|---|
| 955 |
|
|---|
| 956 |
$(LI No macros to inadvertently slam code) |
|---|
| 957 |
|
|---|
| 958 |
$(LI Inline functions instead of macros) |
|---|
| 959 |
|
|---|
| 960 |
$(LI Vastly reduced need for pointers) |
|---|
| 961 |
|
|---|
| 962 |
$(LI Integral type sizes are explicit) |
|---|
| 963 |
|
|---|
| 964 |
$(LI No more uncertainty about the signed-ness of chars) |
|---|
| 965 |
|
|---|
| 966 |
$(LI No need to duplicate declarations in source and header files.) |
|---|
| 967 |
|
|---|
| 968 |
$(LI Explicit parsing support for adding in debug code.) |
|---|
| 969 |
) |
|---|
| 970 |
) |
|---|
| 971 |
|
|---|
| 972 |
$(SECTION4 Compile Time Checks, |
|---|
| 973 |
|
|---|
| 974 |
$(UL |
|---|
| 975 |
$(LI Stronger type checking) |
|---|
| 976 |
|
|---|
| 977 |
$(LI No empty ; for loop bodies) |
|---|
| 978 |
|
|---|
| 979 |
$(LI Assignments do not yield boolean results) |
|---|
| 980 |
|
|---|
| 981 |
$(LI Deprecating of obsolete API's) |
|---|
| 982 |
) |
|---|
| 983 |
) |
|---|
| 984 |
|
|---|
| 985 |
$(SECTION4 Runtime Checking, |
|---|
| 986 |
|
|---|
| 987 |
$(UL |
|---|
| 988 |
$(LI assert() expressions) |
|---|
| 989 |
|
|---|
| 990 |
$(LI array bounds checking) |
|---|
| 991 |
|
|---|
| 992 |
$(LI undefined case in switch exception) |
|---|
| 993 |
|
|---|
| 994 |
$(LI out of memory exception) |
|---|
| 995 |
|
|---|
| 996 |
$(LI In, out, and class invariant Contract Programming support) |
|---|
| 997 |
) |
|---|
| 998 |
) |
|---|
| 999 |
) |
|---|
| 1000 |
|
|---|
| 1001 |
$(SECTION3 Compatibility, |
|---|
| 1002 |
|
|---|
| 1003 |
$(SECTION4 Operator precedence and evaluation rules, |
|---|
| 1004 |
|
|---|
| 1005 |
$(P D retains C operators and their precedence rules, order of |
|---|
| 1006 |
evaluation rules, and promotion rules. This avoids subtle |
|---|
| 1007 |
bugs that might arise from being so used to the way C |
|---|
| 1008 |
does things that one has a great deal of trouble finding |
|---|
| 1009 |
bugs due to different semantics. |
|---|
| 1010 |
) |
|---|
| 1011 |
) |
|---|
| 1012 |
|
|---|
| 1013 |
$(SECTION4 Direct Access to C API's, |
|---|
| 1014 |
|
|---|
| 1015 |
$(P Not only does D have data types that correspond to C types, |
|---|
| 1016 |
it provides direct access to C functions. There is no need |
|---|
| 1017 |
to write wrapper functions, parameter swizzlers, nor code to copy |
|---|
| 1018 |
aggregate members one by one. |
|---|
| 1019 |
) |
|---|
| 1020 |
) |
|---|
| 1021 |
|
|---|
| 1022 |
$(SECTION4 Support for all C data types, |
|---|
| 1023 |
|
|---|
| 1024 |
$(P Making it possible to interface to any C API or existing C |
|---|
| 1025 |
library code. This support includes structs, unions, enums, |
|---|
| 1026 |
pointers, and all C99 types. |
|---|
| 1027 |
D includes the capability to |
|---|
| 1028 |
set the alignment of struct members to ensure compatibility with |
|---|
| 1029 |
externally imposed data formats. |
|---|
| 1030 |
) |
|---|
| 1031 |
) |
|---|
| 1032 |
|
|---|
| 1033 |
$(SECTION4 OS Exception Handling, |
|---|
| 1034 |
|
|---|
| 1035 |
$(P D's exception handling mechanism will connect to the way |
|---|
| 1036 |
the underlying operating system handles exceptions in |
|---|
| 1037 |
an application. |
|---|
| 1038 |
) |
|---|
| 1039 |
) |
|---|
| 1040 |
|
|---|
| 1041 |
$(SECTION4 Uses Existing Tools, |
|---|
| 1042 |
|
|---|
| 1043 |
$(P D produces code in standard object file format, enabling the use |
|---|
| 1044 |
of standard assemblers, linkers, debuggers, profilers, exe compressors, |
|---|
| 1045 |
and other analyzers, as well as linking to code written in other |
|---|
| 1046 |
languages. |
|---|
| 1047 |
) |
|---|
| 1048 |
) |
|---|
| 1049 |
) |
|---|
| 1050 |
|
|---|
| 1051 |
$(SECTION3 Project Management, |
|---|
| 1052 |
|
|---|
| 1053 |
$(SECTION4 Versioning, |
|---|
| 1054 |
|
|---|
| 1055 |
$(P D provides built-in support for generation of multiple versions |
|---|
| 1056 |
of a program from the same text. It replaces the C preprocessor |
|---|
| 1057 |
#if/#endif technique. |
|---|
| 1058 |
) |
|---|
| 1059 |
) |
|---|
| 1060 |
|
|---|
| 1061 |
$(SECTION4 Deprecation, |
|---|
| 1062 |
|
|---|
| 1063 |
$(P As code evolves over time, some old library code gets replaced |
|---|
| 1064 |
with newer, better versions. The old versions must be available |
|---|
| 1065 |
to support legacy code, but they can be marked as $(I deprecated). |
|---|
| 1066 |
Code that uses deprecated versions will be normally flagged |
|---|
| 1067 |
as illegal, but would be allowed by a compiler switch. |
|---|
| 1068 |
This will make it easy for maintenance |
|---|
| 1069 |
programmers to identify any dependence on deprecated features. |
|---|
| 1070 |
) |
|---|
| 1071 |
) |
|---|
| 1072 |
) |
|---|
| 1073 |
) |
|---|
| 1074 |
|
|---|
| 1075 |
|
|---|
| 1076 |
$(SECTION2 Sample D Program (sieve.d), |
|---|
| 1077 |
|
|---|
| 1078 |
-------------------------- |
|---|
| 1079 |
/* Sieve of Eratosthenes prime numbers */ |
|---|
| 1080 |
|
|---|
| 1081 |
import std.stdio; |
|---|
| 1082 |
|
|---|
| 1083 |
bool[8191] flags; |
|---|
| 1084 |
|
|---|
| 1085 |
int main() |
|---|
| 1086 |
{ int i, count, prime, k, iter; |
|---|
| 1087 |
|
|---|
| 1088 |
writefln("10 iterations"); |
|---|
| 1089 |
for (iter = 1; iter <= 10; iter++) |
|---|
| 1090 |
{ count = 0; |
|---|
| 1091 |
flags[] = 1; |
|---|
| 1092 |
for (i = 0; i < flags.length; i++) |
|---|
| 1093 |
{ if (flags[i]) |
|---|
| 1094 |
{ prime = i + i + 3; |
|---|
| 1095 |
k = i + prime; |
|---|
| 1096 |
while (k < flags.length) |
|---|
| 1097 |
{ |
|---|
| 1098 |
flags[k] = 0; |
|---|
| 1099 |
k += prime; |
|---|
| 1100 |
} |
|---|
| 1101 |
count += 1; |
|---|
| 1102 |
} |
|---|
| 1103 |
} |
|---|
| 1104 |
} |
|---|
| 1105 |
writefln("%d primes", count); |
|---|
| 1106 |
return 0; |
|---|
| 1107 |
} |
|---|
| 1108 |
-------------------------- |
|---|
| 1109 |
) |
|---|
| 1110 |
|
|---|
| 1111 |
) |
|---|
| 1112 |
|
|---|
| 1113 |
Macros: |
|---|
| 1114 |
TITLE=Overview |
|---|
| 1115 |
WIKI=Overview |
|---|