forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.dd
572 lines (487 loc) · 17.3 KB
/
index.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
Ddoc
$(D_S D Programming Language,
$(SECTION3 The D programming language. Modern
convenience. Modeling power. Native efficiency.,
$(P $(RED Visit $(LINK2 http://dconf.org/schedule/index.html, dconf.org) for a list of talks from DConf 2013, videos and slides included.))
<div style="text-align: right; font-size: 11px; margin-bottom: -35px; margin-right: 5px; position: relative; z-index: 10000">
<span class="tip">
<a href="http://forum.dlang.org/group/digitalmars.D" target="_blank" class="button">[your code here]</a>
<span>
Got a brief example illustrating D? Submit your code to the digitalmars.D forum specifying "[your code here]" in the title. Upon approval it will be showcased on a random schedule on D's homepage.
</span>
</span>
</div>
----
#!/usr/bin/env rdmd
// Computes average line length for standard input.
import std.stdio;
void main() {
ulong lines = 0;
double sumLength = 0;
foreach (line; stdin.byLine()) {
++lines;
sumLength += line.length;
}
writeln("Average line length: ",
lines ? sumLength / lines : 0);
}
----
D is a language with C-like syntax and static typing. It pragmatically combines
efficiency, control, and modeling power, with safety and programmer productivity.
$(SECTION3 Convenience,
$(UL
$(LI D allows writing large code fragments without redundantly specifying types,
like dynamic languages do. On the other hand, static inference deduces types and other
code properties, giving the best of both the static and the
dynamic worlds. $(EXAMPLE 1,
----
void main() {
// Define an array of numbers, double[]. Compiler recognizes the common
// type of all initializers.
auto arr = [ 1, 2, 3.14, 5.1, 6 ];
// Dictionary that maps string to int, type is spelled int[string]
auto dictionary = [ "one" : 1, "two" : 2, "three" : 3 ];
// Calls the min function defined below
auto x = min(arr[0], dictionary["two"]);
}
// Type deduction works for function results. This is important for generic
// functions, such as min below, which works correctly for all comparable
// types.
auto min(T1, T2)(T1 lhs, T2 rhs) {
return rhs < lhs ? rhs : lhs;
}
----
))
$(LI Automatic memory management makes for safe, simple, and robust code.
D also supports scoped resource management (aka the
$(HTTP en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization, RAII) idiom)
and $(LINK2 statement.html#ScopeGuardStatement, $(D scope) statements) for
deterministic transactional code that is easy to write and read. $(EXAMPLE 2,
----
import std.stdio;
class Widget { }
void main() {
// Automatically managed.
auto w = new Widget;
// Code is executed in any case upon scope exit.
scope(exit) { writeln("Exiting main."); }
// File is closed deterministically at scope's end.
foreach (line; File("text.txt").byLine()) {
writeln(line);
}
writeln();
}
----
)
)
$(LI Built-in linear and associative arrays, slices, and ranges make daily
programming simple and pleasant for tasks, both small and large. $(EXAMPLE 3,
----
#!/usr/bin/env rdmd
import std.range, std.stdio;
// Compute average line length for stdin
void main() {
ulong lines = 0, sumLength = 0;
foreach (line; stdin.byLine()) {
++lines;
sumLength += line.length;
}
writeln("Average line length: ",
lines ? cast(double) sumLength / lines : 0.0);
}
----
))
))
$(SECTION3 Power,
$(UL
$(LI The best paradigm is to not impose something at the expense of others.
D offers classic polymorphism, value semantics, functional
style, generics, generative programming, contract programming,
and more—all harmoniously integrated. $(EXAMPLE 4,
----
// Interfaces and classes
interface Printable {
void print(uint level)
in { assert(level > 0); } // contract is part of the interface
}
// Interface implementation
class Widget : Printable {
void print(uint level)
in{ }
body{ }
}
// Single inheritance of state
class ExtendedWidget : Widget {
override void print(uint level)
in { /* weakening precondition is okay */ }
body {
//... level may be 0 here ...
}
}
// Immutable data shared across threads
immutable string programName = "demo";
// Mutable data is thread-local
int perThread = 42;
// Explicitly shared data
shared int perApp = 5;
// Structs have value semantics
struct BigNum {
// intercept copying
this(this) { }
// intercept destructor
~this() { }
}
void main()
{
// ...
}
----
))
$(LI D offers an innovative approach to concurrency, featuring true
immutable data, message passing, no sharing by default, and
controlled mutable sharing across threads. $(HTTP
informit.com/articles/article.aspx?p=1609144, Read more).)
$(LI From simple scripts to large projects, D has the breadth
to scale with any application's needs: unit testing,
information hiding, refined modularity, fast compilation, precise
interfaces. $(HTTP drdobbs.com/high-performance-computing/217801225,
Read more).)
))
$(SECTION3 Efficiency,
$(UL
$(LI D compiles naturally to efficient native code.)
$(LI D is designed such that most "obvious" code is fast $(I and)
safe. On occasion a function might need to escape the confines of type
safety for ultimate speed and control. For such rare cases D offers
native pointers, type casts, access to any C function without any
intervening translation, and even inline assembly code. $(EXAMPLE 5,
----
import core.stdc.stdlib;
void livingDangerously() {
// Access to C's malloc and free primitives
auto buf = malloc(1024 * 1024);
scope(exit) free(buf); // free automatically upon scope exit
// Interprets memory as an array of floats
auto floats = cast(float[]) buf[0 .. 1024 * 1024];
// Even stack allocation is possible
auto moreBuf = alloca(4096 * 100);
//...
}
// Using inline asm for extra speed on x86
uint checked_multiply(uint x, uint y) {
uint result;
version (D_InlineAsm_X86) {
// Inline assembler "sees" D variables.
asm {
mov EAX,x ;
mul EAX,y ;
mov result,EAX ;
jc Loverflow ;
}
return result;
} else {
result = x * y;
if (!y || x <= uint.max / y)
return result;
}
Loverflow:
throw new Exception("multiply overflow");
}
void main()
{
// ...
}
----
))
$(LI The $(D @safe), $(D @trusted), and $(D @system) modular
attributes allow the programmer to best decide the safety-efficiency
tradeoffs of an application, and have the compiler check for
consistency. $(LINK2 safed.html, Read more.))
)
))
$(XXXXSECTION2 Community,
$(P D is the center of a growing, vibrant community. Get D-related
news from the $(D digitalmars.D.announce) forum $(TAG2 span,
style="font-size:80%", $(HTTP
digitalmars.com/pnews/indexing.php?server=news.digitalmars.com&group=digitalmars.D.announce,
[browse]) $(LINK2 news://news.digitalmars.com/digitalmars.D.announce,
[usenet]) $(HTTP
lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-announce,
[mailing list])). On Twitter, subscribe to D's official announcements
channel, $(HTTP twitter.com/#!/D_Programming,@D_Programming), and
search for (and disseminate) news using tag $(HTTP
twitter.com/#!/search/%23d_lang, #d_lang).)
$(P Learning D and have a question about the best way to do X? The $(D
digitalmars.D.learn) forum $(TAG2 span, style="font-size:80%", $(HTTP
digitalmars.com/pnews/indexing.php?server=news.digitalmars.com&group=digitalmars.D.learn,
[browse]) $(LINK2 news://news.digitalmars.com/digitalmars.D.learn,
[usenet]) $(HTTP
lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn,
[mailing list])) is the hangout place where many D experts are ready
to help fellow hackers.)
$(P The $(D digitalmars.D) forum $(TAG2 span, style="font-size:80%",
$(HTTP
digitalmars.com/pnews/indexing.php?server=news.digitalmars.com&group=digitalmars.D,
[browse]) $(LINK2 news://news.digitalmars.com/digitalmars.D, [usenet])
$(HTTP lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d,
[mailing list])) is the best place to discuss anything and everything
related to D: language design ideas, suggestions, status and future,
contributions to the language and its standard library. Reach and
engage all major D contributors, including the very creator of D,
$(HTTP walterbright.com, Walter Bright). For real-time conversation, use #d IRC channel on freenode.)
$(SECTION3 To contribute,
$(P Many D enthusiasts are not only using, but also contributing to
the language, its implementation, and its standard library. The main
hub of D development is $(HTTP github.com/D-Programming-Language,
D-Programming-Language on github). The project has been awash in
contributions ever since creating the github repository on January 23,
2011, with an average of over 3 pull requests per day. (That doesn't
mean there's not a lot more to do!) You are welcome to fork any
subproject ($(HTTP github.com/D-Programming-Language/dmd, compiler),
$(HTTP github.com/D-Programming-Language/druntime, runtime), $(HTTP
github.com/D-Programming-Language/phobos, standard library), $(HTTP
github.com/D-Programming-Language/dlang.org,
website), $(HTTP github.com/D-Programming-Language/tools, tools), or
$(HTTP github.com/D-Programming-Language/installer, installer)), change
it in useful ways, and propose your changes back to the community.
$(P The reference compiler $(LINK2 download.html, dmd) has inspired
work on alternate implementations that add value by using distinct
back-ends:)
$(UL
$(LI GNU D compiler $(LINK2 http://bitbucket.org/goshawk/gdc/wiki/Home, gdc).)
$(LI LLVM D Compiler $(HTTP dsource.org/projects/ldc, ldc).)
)
$(P The gdc compiler is of particular interest because it taps into
gcc's extremely portable back-end and large installation base. The D
development team has signed the appropriate documents with FSF and is
pursuing integration of gdc with gcc version 4.8 starting March 2012.
$(HTTPS launchpad.net/~ibuclaw, Iain Buclaw) is leading that
effort.)
)
)
)
)
$(COMMENT
$(COMMENT $(AMAZONLINK 0321635361, <img style="float:right;
padding-left:1.5em; padding-bottom:1.5em; border-width: 0;"
src="images/tdpl.jpg" alt="The D Programming Language book" />))
$(P The K&R of D is here! Get $(AMAZONLINK 0321635361, The D
Programming Language) by Andrei Alexandrescu—the authoritative
source on everything D. From the book's introduction:
$(BLOCKQUOTE $(AMAZONLINK 0321635361, The D Programming Language) by
Andrei Alexandrescu, D is a language that attempts to consistently do
the right thing within the constraints it chose: system-level access
to computing resources, high performance, and syntactic similarity
with C-derived languages. In trying to do the right thing, D
sometimes stays with tradition and does what other languages do, and
other times it breaks tradition with a fresh, innovative solution. On
occasion that meant revisiting the very constraints that D ostensibly
embraced. For example, large program fragments or indeed entire
programs can be written in a well-defined memory-safe subset of D,
which entails giving away a small amount of system-level access for a
large gain in program debuggability.))
$(P D is a multi-paradigm programming language that combines a
principled approach with a focus on practicality. In D you get to
harness the power and high performance of C and C++ and also the
safety and programmer productivity of modern languages such as Ruby
and Python. Special attention is given to the needs of quality
assurance, documentation, portability, and reliability.)
$(P The D language is statically typed and compiles directly to machine code.
It’s multiparadigm, supporting many programming styles:
imperative, object oriented, and metaprogramming. It’s a member of the C
syntax family, and its appearance is very similar to that of C++.
Here’s a quick list of $(LINK2 comparison.html, features).
)
$(P This site does not refer to the first edition of the language,
known as "D version 1" or "D1". Currently, D1 is in maintenance mode
and has documentation available $(LINK2
http://www.digitalmars.com/d/1.0/index.html,here). We recommend the
current edition of the D programming language (formerly known as "D2")
for new projects. )
$(P There are currently three implementations:
$(OL
$(LI Digital Mars dmd for
$(LINK2 dmd-windows.html, Windows),
$(LINK2 dmd-linux.html, x86 Linux),
$(LINK2 dmd-osx.html, Mac OS X), and
$(LINK2 dmd-freebsd.html, x86 FreeBSD).
)
$(LI LLVM D Compiler $(LINK2 http://www.dsource.org/projects/ldc, ldc).
)
$(COMMENT
$(LI Gnu D compiler $(LINK2 http://dgcc.sourceforge.net/, gdc)
for several platforms, including
$(LINK2 http://gdcwin.sourceforge.net/, Windows) and
$(LINK2 http://gdcmac.sourceforge.net/, Mac OS X)
for D versions 1.030 and 2.014.
)
)
$(LI Gnu D compiler $(LINK2 http://bitbucket.org/goshawk/gdc/wiki/Home, gdc).
)
$(COMMENT $(LINK2 http://dnet.codeplex.com/, D.NET compiler)
alpha for .NET for D version 2.)
)
)
$(P A large and growing collection of D source code and projects
are at $(LINK2 http://www.dsource.org, dsource).
More links to innumerable D wikis, libraries, tools, media articles,
etc. are at $(LINK2 http://www.digitalmars.com/d/dlinks.html, dlinks).
)
$(COMMENT $(P
This document is available as a
$(LINK2 http://www.prowiki.org/wiki4d/wiki.cgi?LanguageSpecification/PDFArchive, pdf),
as well as in
$(LINK2 http://www.kmonos.net/alang/d/, Japanese)
and
$(LINK2 http://elderane.50webs.com/tuto/d/, Portugese)
translations.
A German book
$(LINK2 http://www.amazon.de/Programmieren-D-Einf%C3%BChrung-neue-Programmiersprache/dp/3939084697/, Programming in D: Introduction to the new Programming Language)
is available, as well as
a Japanese book
$(LINK2 http://www.gihyo.co.jp/books/syoseki-contents.php/4-7741-2208-4, D Language Perfect Guide),
and a Turkish book
$(LINK2 http://ddili.org/ders/d/, D Programlama Dili Dersleri).
))
$(COMMENT: Japanese by Kazuhiro Inaba, Portugese by Christian Hartung)
$(P This is an example D program illustrating some of the capabilities:)
----
#!/usr/bin/env rdmd
/* shebang is supported */
/* Hello World in D
To compile:
dmd hello.d (or on Unix just make hello.d executable and run it)
or to optimize:
dmd -O -inline -release hello.d
*/
import std.stdio;
void main(string[] args)
{
writeln("Hello World, Reloaded");
// auto type inference and built-in foreach
foreach (argc, argv; args)
{
// Object Oriented Programming
auto cl = new CmdLin(argc, argv);
// Improved typesafe printf
writeln(cl.argnum, cl.suffix, " arg: ", cl.argv);
// Automatic or explicit memory management
delete cl;
}
// Nested structs and classes
struct specs
{
// all members automatically initialized
size_t count, allocated;
}
// Nested functions can refer to outer
// variables like args
specs argspecs()
{
specs* s = new specs;
// no need for '->'
s.count = args.length; // get length of array with .length
s.allocated = typeof(args).sizeof; // built-in native type properties
foreach (argv; args)
s.allocated += argv.length * typeof(argv[0]).sizeof;
return *s;
}
// built-in string and common string operations
writefln("argc = %d, " ~ "allocated = %d",
argspecs().count, argspecs().allocated);
}
class CmdLin
{
private size_t _argc;
private string _argv;
public:
this(size_t argc, string argv) // constructor
{
_argc = argc;
_argv = argv;
}
size_t argnum()
{
return _argc + 1;
}
string argv()
{
return _argv;
}
string suffix()
{
string suffix = "th";
switch (_argc)
{
case 0:
suffix = "st";
break;
case 1:
suffix = "nd";
break;
case 2:
suffix = "rd";
break;
default:
break;
}
return suffix;
}
}
----
$(P $(B Notice:) We welcome feedback about the D compiler or language, but please be explicit about
any claims to intellectual property rights with a copyright or patent notice if you have such for
your contributions. We want D to remain open and free to use, and do not wish to be caught by
someone posting a patch to the compiler, and then later claim compensation for that work.)
)
Macros:
TITLE=Home
WIKI=Intro
TAG=<$1>$+</$1>
TAG2=<$1 $2>$3</$1>
D=<span class="d_inlinecode">$0</span>
EXAMPLE=
<script>
document.write('$(TAG2 div, id="q$1" class="question" onclick="showHideAnswer(this);", <span class="nobr">See example.</span>)');
</script>
<noscript><span class="nobr">See example.</span></noscript>
$(TAG2 div, id="a$1" class="answer-nojs", $2)
LAYOUT=
<div id="navigation">
$1
$(GOOGLE_TRANSLATE)
</div>
<div id="twitter">
<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
version : 2,
type : 'profile',
rpp : 8,
interval : 30000,
width : 'auto',
height : 600,
theme : {
shell : {
background : '#1f252b',
//color : '#000000'
},
tweets : {
//background : '',
//color : '',
//links : ''
}
},
features : {
scrollbar : true,
loop : false,
live : true,
behavior : 'all'
}
}).render().setUser('D_programming').start();
</script>
</div>
<div id="content" style="margin-right:18em;" class='hyphenate'>
$(PAGE_TOOLS)
$3
</div>