forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.dd
608 lines (524 loc) · 19.6 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
Ddoc
$(D_S D Programming Language,
$(SECTION3 プログラミング言語 D -
利便性・表現力・効率性)
<div style="text-align: right; font-size: 11px; margin-bottom: -35px; margin-right: 5px; position: relative; z-index: 10000">
<a href="http://forum.dlang.org/group/digitalmars.D" target="_blank" class="tip button">
[your code here]
<span>
Dの特徴を示す良い例を思いつきましたか? digitalmars.D フォーラムに "[your code here]" をタイトルに入れて投稿して下さい。承認が得られれば、D のホームページに掲載されるコードリストに入ります!
</span>
</a>
</div>
$(D_RUN_CODE
$(ARGS
----
#!/usr/bin/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);
}
----
),
$(ARGS Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris tristique rutrum sem, nec convallis enim bibendum ut.
Donec ac dolor tortor, sit amet consequat turpis. In hac habitasse platea dictumst.
Fusce lacus dolor, sodales ac consequat eu, ultricies et leo.
In commodo scelerisque urna non posuere. Sed justo ipsum, consectetur nec sodales in, faucibus ut nunc.
Phasellus nunc metus, mollis eu malesuada at, scelerisque eget nulla.),
$(ARGS))
D は C言語風の構文を持つ静的型付け言語です。
効率性と高い表現力を、安全性とプログラマの生産性に実用的に結びつけます。
$(SECTION3 利便性,
$(UL
$(LI D では、動的言語のように、冗長な型指定なしで長いコード片を記述することができます。
それでも、静的な推論によって型やその他のコードの性質は導出されるため、
静的な世界と動的な世界の両方の恩恵を受けることができます。
$(EXAMPLE 1,
$(D_RUN_CODE
$(ARGS
----
void main() {
// 数値の配列 double[] を定義。
// コンパイラが初期化子の共通型を認識します。
auto arr = [ 1, 2, 3.14, 5.1, 6 ];
// 文字列を整数に写す辞書。型を書くと int[string] です。
auto dictionary = [ "one" : 1, "two" : 2, "three" : 3 ];
// 下で定義される min 関数の呼び出し
auto x = min(arr[0], dictionary["two"]);
}
// 型の導出は関数の返値にも働きます。これは、
// 以下の min など全ての比較可能な型に対して動作するような、
// 総称的な関数に対して特に重要です。
auto min(T1, T2)(T1 lhs, T2 rhs) {
return rhs < lhs ? rhs : lhs;
}
----
),
$(ARGS), $(ARGS))
))
$(LI 自動的なメモリ管理が安全で簡潔で頑強なコードに役立ちます。
D は同時にスコープによるリソース管理 (別名
$(WEB en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization, RAII) 技法)
と $(LINK2 statement.html#ScopeGuardStatement, $(D scope) 文) もサポートし、
決定的なトランザクション的コードを簡単に読み書きできるようにしています。$(EXAMPLE 2,
$(D_RUN_CODE
$(ARGS
----
import std.stdio;
class Widget { }
void main() {
// 自動管理
auto w = new Widget;
// スコープ終了時に必ず実行されるコード
scope(exit) { writeln("Exiting main."); }
// Fileはスコープ終了時に決定的に閉じられる
foreach (line; File("text.txt").byLine()) {
writeln(line);
}
writeln();
}
----
),
$(ARGS), $(ARGS))
)
)
$(LI 組み込みの配列と連想配列、スライス、そしてレンジの機能が、
日々のプログラミングを簡単で、小規模から大規模まで種々のタスクを楽しいものにします。$(EXAMPLE 3,
$(D_RUN_CODE
$(ARGS
----
#!/usr/bin/rdmd
import std.range, std.stdio;
// 標準入力の平均行数を計算
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);
}
----
),
$(ARGS Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris tristique rutrum sem, nec convallis enim bibendum ut.
Donec ac dolor tortor, sit amet consequat turpis. In hac habitasse platea dictumst.
Fusce lacus dolor, sodales ac consequat eu, ultricies et leo.
In commodo scelerisque urna non posuere. Sed justo ipsum, consectetur nec sodales in, faucibus ut nunc.
Phasellus nunc metus, mollis eu malesuada at, scelerisque eget nulla.),
$(ARGS))
))
))
$(SECTION3 表現力,
$(UL
$(LI 最高のパラダイムとは、他のパラダイムに犠牲を強いることが無いことです。
D は古典的な多態性、値セマンティクス、関数型スタイル、
総称性、生成的プログラミング、契約プログラミング、
その他様々な手法を提供します—すべてがハーモニーを奏でる形で。. $(EXAMPLE 4,
$(D_RUN_CODE
$(ARGS
----
// インターフェイスとクラス
interface Printable {
void print(uint level)
in { assert(level > 0); } // 契約
}
// インターフェイスの実装
class Widget : Printable {
void print(uint level)
in{ }
body{ }
}
// 単一継承
class ExtendedWidget : Widget {
override void print(uint level)
in { /* 事前条件の緩和 */ }
body {
//... level may be 0 here ...
}
}
// 不変データのスレッド間共有
immutable string programName = "demo";
// 可変データはスレッドローカル
int perThread = 42;
// 明示的な共有
shared int perApp = 5;
// 構造体は値セマンティクス
struct BigNum {
// コピーへの割り込み
this(this) { ... }
// デストラクタへの割り込み
~this() { ... }
}
void main()
{
// ...
}
----
),
$(ARGS), $(ARGS))
))
$(LI Dは並行性へのイノベーティブなアプローチを提供します。
真に変更不可能なデータ、メッセージパッシング、スレッド間での、デフォルトでのデータ非共有、
そして制御された変更可能な共有データなどがあります。$(WEBX
informit.com/articles/article.aspx?p=1609144, 詳しくはこちら)。)
$(LI 簡単なスクリプトから大規模なプロジェクトまで、
Dはどんなアプリケーションの要求にもスケールする幅広さを備えています: 単体テスト、
情報の隠蔽、洗練されたモジュール性、高速なコンパイル、
正確なインターフェイス、など。$(WEBX drdobbs.com/high-performance-computing/217801225,
詳しくはこちら)。)
))
$(SECTION3 効率性,
$(UL
$(LI D は自然に効率的なネイティブコードにコンパイルされます。)
$(LI D は最も"自然な"コードが高速で $(I しかも) 安全に動作するよう設計されています。
時には、型安全性の保証を回避して、最高速度と完全な制御を必要とする機能もあるでしょう。
そのようなレアケースのために、
D は生のポインタや型キャスト、一切の橋渡し処理なしのC関数の直接呼び出し、
そしてインラインアセンブラをもサポートしています。$(EXAMPLE 5,
$(D_RUN_CODE
$(ARGS
----
import core.stdc.stdlib;
void livingDangerously() {
// C の mallo と free を使用
auto buf = malloc(1024 * 1024);
scope(exit) free(buf); // スコープ終了時の自動解放
// メモリを float の配列と解釈
auto floats = cast(float[]) buf[0 .. 1024 * 1024];
// スタック割り当てすらも可能
auto moreBuf = alloca(4096 * 100);
//...
}
// x86 でさらなる速度を得るためのインラインアセンブラの使用
uint checked_multiply(uint x, uint y) {
uint result;
version (D_InlineAsm_X86) {
// インラインアセンブラからDの変数が"見える"
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()
{
// ...
}
----
),
$(ARGS), $(ARGS))
))
$(LI $(D @safe), $(D @trusted), $(D @system) の3つのモジュール属性によって、
プログラマは安全性と効率性のトレードオフをアプリケーション毎に最適に選択することができ、
その一貫性をコンパイラに検査させることができます。
$(LINK2 safed.html, 詳しくはこちら)。)
)
))
$(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%", $(WEB
digitalmars.com/pnews/indexing.php?server=news.digitalmars.com&group=digitalmars.D.announce,
[browse]) $(LINK2 news://news.digitalmars.com/digitalmars.D.announce,
[usenet]) $(WEB
lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-announce,
[mailing list])). On Twitter, subscribe to D's official announcements
channel, $(WEB twitter.com/#!/D_Programming,@D_Programming), and
search for (and disseminate) news using tag $(WEB
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%", $(WEB
digitalmars.com/pnews/indexing.php?server=news.digitalmars.com&group=digitalmars.D.learn,
[browse]) $(LINK2 news://news.digitalmars.com/digitalmars.D.learn,
[usenet]) $(WEB
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%",
$(WEB
digitalmars.com/pnews/indexing.php?server=news.digitalmars.com&group=digitalmars.D,
[browse]) $(LINK2 news://news.digitalmars.com/digitalmars.D, [usenet])
$(WEB 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,
$(WEB 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 $(WEB 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 ($(WEB github.com/D-Programming-Language/dmd, compiler),
$(WEB github.com/D-Programming-Language/druntime, runtime), $(WEB
github.com/D-Programming-Language/phobos, standard library), $(WEB
github.com/D-Programming-Language/d-programming-language.org,
website), $(WEB github.com/D-Programming-Language/tools, tools), or
$(WEB 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 $(WEB 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.
$(WEB 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/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)
{
$(V1 writefln("Hello World, Reloaded");)$(V2 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>
WEB=$(LINK2 http://$1,$2)
WEBX=$(LINK2 http://$1,$2)
D=<span class="d_inlinecode">$0</span>
EXAMPLE=
<script>
document.write('$(TAG2 div, id="q$1" class="question" onclick="showHideAnswer(this);", <span class="nobr">(例を表示)</span>)');
</script>
<noscript><span class="nobr">例をご覧下さい: </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>