-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
index.dd
1047 lines (942 loc) · 29 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ddoc
$(D_S D Programming Language,
$(SECTION1 ,
$(DIVC intro, $(DIV, $(DIV,
$(DIV,
$(TC p, pitch, $(B D) is a general-purpose programming language with
static typing, systems-level access, and C-like syntax.
With the $(B D Programming Language), write fast,
read fast, and run fast.
$(BR)$(BR)Fast code, fast.)
$(BR)
$(DIVC download,
$(DIVC btn-group-vertical,
<a href="download.html" class="btn">Downloads</a>
$(SPANC smallprint, Latest version: $(LATEST)
– $(LINK2 changelog/$(LATEST).html, Changelog))
)
)
)
$(DIVID your-code-here,
$(DIVCID fa-select, your-code-here-select-example,)
$(DIVC tip,
$(LINK2 https://forum.dlang.org/newpost/general?subject=%5Byour+code+here%5D, your code here)
$(DIV,
$(P Got a brief example illustrating D?)
$(P Submit your code to the digitalmars.D forum specifying
"[your code here]" in the subject.)
$(P Upon approval it will be showcased here on a random schedule.)
)
)
$(EXTRA_EXAMPLE_RUNNABLE Compute average line length for stdin,
$(RUNNABLE_EXAMPLE_STDIN
The D programming language
Modern convenience.
Modeling power.
Native efficiency.)
----
void main()
{
import std.range, std.stdio;
auto sum = 0.0;
auto count = stdin
//Get an input range set up to read one line at a time
.byLine
//Perform a transparent operation (as in the shell command tee)
.tee!(l => sum += l.length)
.walkLength;
writeln("Average line length: ",
count ? sum / count : 0);
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Round floating point numbers,
$(RUNNABLE_EXAMPLE_STDIN 2.4 plus 2.4 equals 5 for sufficiently large values of 2.)
----
import std.algorithm, std.conv, std.functional,
std.math, std.regex, std.stdio;
alias round = pipe!(to!real, std.math.round, to!string);
static reFloatingPoint = ctRegex!`[0-9]+\.[0-9]+`;
void main()
{
// Replace anything that looks like a real
// number with the rounded equivalent.
stdin
.byLine
.map!(l => l.replaceAll!(c => c.hit.round)
(reFloatingPoint))
.each!writeln;
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Sort lines,
$(RUNNABLE_EXAMPLE_STDIN
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune)
----
import std.stdio, std.array, std.algorithm;
void main()
{
stdin
.byLineCopy
.array
.sort!((a, b) => a > b) // descending order
.each!writeln;
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Sort an Array at Compile-Time,
----
void main()
{
import std.algorithm, std.stdio;
"Starting program".writeln;
enum a = [ 3, 1, 2, 4, 0 ];
// Sort data at compile-time
static immutable b = sort(a);
// Print the result _during_ compilation
pragma(msg, "Finished compilation: ", b);
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Invoke external programs,
----
void main()
{
import std.exception, std.stdio, std.process;
auto result = ["whoami"].execute;
enforce(result.status == 0);
result.output.write;
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Print hex dump,
----
void main()
{
import std.algorithm, std.stdio, std.file, std.range;
enum cols = 14;
// Split file into 14-byte chunks per row
thisExePath.File("rb").byChunk(cols).take(20).each!(chunk =>
// Use range formatting to format the
// hexadecimal part and align the text part
writefln!"%(%02X %)%*s %s"(
chunk,
3 * (cols - chunk.length), "", // Padding
chunk.map!(c => // Replace non-printable
c < 0x20 || c > 0x7E ? '.' : char(c))));
}
----
)
$(EXTRA_EXAMPLE Start a minimal web server,
----
#!/usr/bin/env dub
/+ dub.sdl:
dependency "vibe-d" version="~>0.9.0"
+/
void main()
{
import vibe.d;
listenHTTP(":8080", (req, res) {
res.writeBody("Hello, World: " ~ req.path);
});
runApplication();
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Initialize an Array in parallel,
----
void main()
{
import std.datetime.stopwatch : benchmark;
import std.math, std.parallelism, std.stdio;
auto logs = new double[100_000];
auto bm = benchmark!({
foreach (i, ref elem; logs)
elem = log(1.0 + i);
}, {
foreach (i, ref elem; logs.parallel)
elem = log(1.0 + i);
})(100); // number of executions of each tested function
writefln("Linear init: %s msecs", bm[0].total!"msecs");
writefln("Parallel init: %s msecs", bm[1].total!"msecs");
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Sort in-place across multiple arrays,
----
void main()
{
import std.stdio : writefln;
import std.algorithm.sorting : sort;
import std.range : chain;
int[] arr1 = [4, 9, 7];
int[] arr2 = [5, 2, 1, 10];
int[] arr3 = [6, 8, 3];
// @nogc functions are guaranteed by the compiler
// to be without any GC allocation
() @nogc {
sort(chain(arr1, arr2, arr3));
}();
writefln("%s\n%s\n%s\n", arr1, arr2, arr3);
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Count frequencies of character pairs,
----
void main()
{
import std.stdio : writefln;
// An associative array mapping pairs of characters to integers
int[char[2]] aa;
auto arr = "ABBBA";
// Iterate over all pairs in the string
// ['A', 'B'], ['B', 'B'], ..., ['B', 'A']
foreach (i; 0 .. arr.length - 1)
{
// String slicing doesn't allocate a copy
char[2] pair = arr[i .. i + 2];
// count occurrences
aa[pair]++;
}
foreach (key, value; aa)
writefln("key: %s, value: %d", key, value);
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Tiny RPN calculator,
$(RUNNABLE_EXAMPLE_STDIN 2 3 3 4 + * *)
----
void main()
{
import std.stdio, std.string, std.algorithm, std.conv;
// arr is real[] and sym is the current symbol
readln.split.fold!((arr, sym)
{
static foreach (c; "+-*/")
if (sym == [c])
// replace the last 2 elements with the binary op
return arr[0 .. $-2] ~
mixin("arr[$-2] " ~ c ~ " arr[$-1]");
// sym must be a number
return arr ~ sym.to!real;
})((real[]).init).writeln;
}
----
)
$(EXTRA_EXAMPLE_RUNNABLE Subtyping with alias this,
----
struct Point
{
private double[2] p;
// Forward all undefined symbols to p
alias p this;
double dot(Point rhs)
{
return p[0] * rhs.p[0] + p[1] * rhs.p[1];
}
}
void main()
{
import std.stdio : writeln;
// Point behaves like a `double[2]` ...
Point p1, p2; p1 = [2, 1], p2 = [1, 1];
assert(p1[$ - 1] == 1);
// ... but with extended functionality
writeln("p1 dot p2 = ", p1.dot(p2));
}
----
)
) $(COMMENT your-code-here)
))) $(COMMENT intro, div, div)
$(COMMENT
Picking the frontpage example after the page has loaded leads to reflows
and re-rendering. Hence, we pick the example as soon as possible
)
$(SCRIPT
(function() {
// randomly pick one example and bootstrap the runnable editor
var examples = document.getElementsByClassName('your-code-here-extra');
var rouletteIndex = Math.floor(Math.random() * examples.length);
var rouletteChild = examples[rouletteIndex];
rouletteChild.style.display = "block";
// build a list of the titles of all examples and add an option chooser to
// allow switching between them
var titles = Array.prototype.map.call(examples, function(e) {
return e.getElementsByClassName("your-code-here-title")[0].innerText;
});
var sel = document.createElement("select");
Array.prototype.forEach.call(titles, function(title, i) {
var el = document.createElement("option");
el.value = i;
el.innerText = title;
sel.appendChild(el);
});
sel.selectedIndex = rouletteIndex;
sel.addEventListener("change", function(e) {
examples[rouletteIndex].style.display = "none";
rouletteIndex = sel.selectedIndex;
examples[rouletteIndex].style.display = "block";
});
var selEl = document.getElementById("your-code-here-select-example");
selEl.appendChild(sel);
})();
)
$(DIVC call_to_donate,
<h2>Support the D language</h2>
<p class="center">D is made possible through the hard work and dedication of many volunteers,
with the coordination and outreach of the D Language Foundation, a 501(c)(3) non-profit organization.
You can help further the development of the D language and help grow our
community by supporting the Foundation.
</p>
<p class="center">
<a href="$(ROOT_DIR)foundation/donate.html" class="btn call_to_donate_btn">Donate</a>
<a href="$(ROOT_DIR)foundation/index.html" class="btn subdued">Learn More About The Foundation</a>
$(BR)
$(BR)
Lots of $(WHY_D_ICON heart) to our $(LINK2 $(ROOT_DIR)foundation/sponsors.html, sponsors) and
$(LINK2 $(ROOT_DIR)foundation/contributors.html, contributors).
</p>
)
$(DIVC org_list_header,
<h3 class="center">
Industry Proven
</h3>
)
$(DIVC org_list,
<a href="https://medium.com/@NetflixTechBlog/introducing-vectorflow-fe10d7f126b8"><img src="$(ROOT_DIR)images/orgs-using-d/netflix_small.png" /></a>
<a href="https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/"><img src="$(ROOT_DIR)images/orgs-using-d/ebay.jpg" /></a>
<a href="https://dconf.org/2019/talks/beer.html"><img src="$(ROOT_DIR)images/orgs-using-d/funkwerk.png" /></a>
<a href="https://dconf.org/2019/talks/colvin.html"><img src="$(ROOT_DIR)images/orgs-using-d/symmetry.png" /></a>
<a href="https://dlang.org/blog/2016/07/07/project-highlight-auburn-sounds/"><img src="$(ROOT_DIR)images/orgs-using-d/auburn.png" /></a>
<a href="https://dconf.org/2016/talks/zvibel.html"><img src="$(ROOT_DIR)images/orgs-using-d/weka.png" /></a>
)
$(DIVC org_list_footer,
<div class="center">
<div id="front-testimonial">
<div id="front-testimonial-quote">
$(FA_ICON quote-left) D shines from low-level control
to high-level abstraction $(FA_ICON quote-right) $(BR)
</div>
$(FA_ICON heart) $(LINK2 $(ROOT_DIR)orgs-using-d.html, Success stories)
$(FA_ICON history) $(LINK2 $(ROOT_DIR)areas-of-d-usage.html, What is D used for?)
</div>
</div>
)
$(DIVC more,
$(DIVID news,
$(DIVID forum-summary,
<iframe src="https://forum.dlang.org/frame-announcements"></iframe>
<iframe src="https://forum.dlang.org/frame-discussions"></iframe>
)
)
$(DIVC boxes,
$(DIVC row,
$(TOUR newspaper-o, News,
$(P Stay updated with the latest posts in the
$(LINK2 http://blog.dlang.org,
<cite>Official D Blog</cite>) from $(DBLOG_LATEST_DATE_1):
$(LINK2 $(DBLOG_LATEST_LINK_1), $(DBLOG_LATEST_TITLE_1)) by
$(DBLOG_LATEST_AUTHOR_1).
)
$(P
From $(DBLOG_LATEST_DATE_2):
$(LINK2 $(DBLOG_LATEST_LINK_2), $(DBLOG_LATEST_TITLE_2)) by
$(DBLOG_LATEST_AUTHOR_2).
)
)
$(TOUR graduation-cap, Learn,
$(P Take the $(LINK2 https://tour.dlang.org,
Tour), explore
$(LINK2 $(ROOT_DIR)overview.html, major features) in D,
browse the $(LINK2 $(ROOT_DIR)comparison.html, quick overview),
start with $(LINK2 $(ROOT_DIR)ctod.html, C) or
$(LINK2 $(ROOT_DIR)cpptod.html, C++) background,
and ask questions in the
$(LINK2 https://forum.dlang.org/group/digitalmars.D.learn,
Learn forum).
)
$(P For a deeper dive into D
check out $(LINK2 https://wiki.dlang.org/Books, books) or
$(LINK2 https://wiki.dlang.org/Videos, videos)
such as Ali Çehreli's free book
$(LINK2 http://ddili.org/ders/d.en/index.html,
<cite>Programming in D</cite>).
)
)
)
$(DIVC row,
$(TOUR comments, Community,
$(P Discuss D on the $(LINK2 https://forum.dlang.org/, forums), join
the $(LINK2 irc://irc.libera.chat/d, IRC channel), read our
$(LINK2 https://dlang.org/blog, official Blog), or follow us
on $(LINK2 https://twitter.com/D_Programming, Twitter).
Browse the $(LINK2 https://wiki.dlang.org/, wiki), where among
other things you can find the
$(LINK2 https://wiki.dlang.org/Vision/2018H1, high-level vision)
of the $(LINK2 $(ROOT_DIR)foundation_overview.html, D Language Foundation).
)
)
$(TOUR book, Documentation,
$(P Refer to the
$(LINK2 $(ROOT_DIR)spec/spec.html, language specification) and
the documentation of
$(LINK2 $(ROOT_DIR)phobos/index.html, Phobos), D's standard
library. The
$(LINK2 $(ROOT_DIR)dmd.html, DMD manual) tells you how
to use the compiler. Read
$(LINK2 $(ROOT_DIR)articles.html, various articles) to deepen
your understanding.
)
)
)
$(DIVC row,
$(TOUR code-fork, Contribute,
$(P Report any bugs you find to our $(LINK2 $(ROOT_DIR)bugstats.html,
bug tracker). If you can fix an issue, make a pull request on
$(LINK2 https://github.com/dlang, GitHub).
There are $(LINK2 https://wiki.dlang.org/Get_involved, many other ways)
to help, too!
)
)
$(TOUR cubes, Packages,
$(P DUB is the package manager for D.
$(LINK2 https://code.dlang.org/getting_started,
Get started with DUB), and check out the
$(LINK2 https://code.dlang.org/, available packages).
)
)
)
$(DIVC row,
$(TOUR play, Run,
$(P Configure linting,
formatting or
completion for
your favorite $(LINK2 https://wiki.dlang.org/IDEs, IDE),
$(LINK2 https://wiki.dlang.org/Editors, editor) or
use $(LINK2 https://run.dlang.io, run.dlang.io) to play and experiment
with D code.
)
)
$(TOUR forward, Explore,
$(P
Learn about $(LINK2 https://qznc.github.io/d-tut, pragmatic D),
the $(LINK2 $(ROOT_DIR)dstyle.html, DStyle),
$(LINK2 https://p0nce.github.io/d-idioms, common D idioms) and
$(LINK2 https://github.com/PhilippeSigaud/D-templates-tutorial, templates),
See what's coming upcoming with $(LINK2 $(ROOT_DIR)changelog/pending.html, next version),
explore $(LINK2 https://github.com/dlang/DIPs, D Improvement Proposals),
and don't fear $(LINK2 https://dlang.org/blog/the-gc-series, D's garbage collection).
)
)
)
)
$(DIVC whyd, $(SECTION2 Fast code$(COMMA) fast.,
$(DIVC section,
$(DIV, $(SECTION3 $(WHY_D_ICON magic) Write Fast,
$(P 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;
}
----
))
$(P Automatic memory management makes for safe, simple, and robust code.
D also supports scoped resource management (aka the
$(HTTPS en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization, RAII) idiom)
and $(LINK2 spec/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(__FILE_FULL_PATH__).byLine())
{
writeln(line);
}
writeln();
}
----
)
)
$(P Built-in linear and associative arrays, slices, and ranges make daily
programming simple and pleasant for tasks, both small and large. $(EXAMPLE 3,
$(RUNNABLE_EXAMPLE_STDIN
The D programming language
Modern convenience.
Modeling power.
Native efficiency.)
----
// Compute average line length for stdin
void main()
{
import std.range, std.stdio;
auto sum = 0.0;
auto count = stdin.byLine
.tee!(l => sum += l.length).walkLength;
writeln("Average line length: ",
count ? sum / count : 0);
}
----
))
)))
$(DIVC section, $(DIV, $(SECTION3 $(WHY_D_ICON rocket) Read Fast,
$(P 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)
// contract is part of the interface
in { assert(level > 0); }
}
// Interface implementation
class Widget : Printable
{
void print(uint level)
in{ }
do{ }
}
// Single inheritance of state
class ExtendedWidget : Widget
{
override void print(uint level)
in { /* weakening precondition is okay */ }
do
{
//... 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()
{
// ...
}
----
))
$(P 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).)
$(P 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).)
)))
$(DIVC section, $(DIV, $(SECTION3 $(WHY_D_ICON bolt) Run Fast,
$(P D compiles naturally to efficient native code.)
$(P 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, manual memory management, custom allocators
and even inline assembly code. $(EXAMPLE 5,
----
import core.stdc.stdlib;
void livingDangerously()
{
// Access to C's malloc and free primitives
enum bytes = float.sizeof * 1024 * 1024;
auto buf = malloc(bytes);
// free automatically upon scope exit
scope(exit) free(buf);
// Interprets memory as an array of floats
auto floats = cast(float[]) buf[0 .. bytes];
// 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 and labels.
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()
{
// ...
}
----
))
$(P The $(D @safe), $(D @trusted), and $(D @system) function
attributes allow the programmer to best decide the safety-efficiency
tradeoffs of an application, and have the compiler check for
consistency. $(LINK2 spec/memory-safe-d.html, Read more).)
)))
)) $(COMMENT SECTION2 Why D?, DIVC whyd)
) $(COMMENT more)
) $(COMMENT SECTION1)
) $(COMMENT D_S)
Macros:
TITLE=Home
TAG=<$1>$+</$1>
TAG2=<$1 $2>$3</$1>
D=<span class="d_inlinecode">$0</span>
EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $(RUNNABLE_EXAMPLE $2))
EXTRA_EXAMPLE=<div class="your-code-here-extra" style="display:none">
$(DIVC your-code-here-title, $1)
$+
</div>
EXTRA_EXAMPLE_RUNNABLE=$(EXTRA_EXAMPLE $1, $(RUNNABLE_EXAMPLE $+))
_=
LAYOUT_PREFIX=
LAYOUT_SUFFIX=
$(SCRIPTLOAD $(ROOT_DIR)js/platform-downloads.js, data-latest="$(LATEST)")
LAYOUT_TITLE=
TOUR=$(DIVC item, $(SECTION4 $(LINK2 $1, $(TC i, fa fa-$2 big-icon)$3), $4))
TOUR=$(DIVC item, $(SECTION4 $(TC i, fa fa-$1 big-icon)$2, $3))
WHY_D_ICON=$(TC i, fa fa-$1 why-d-icon)
_=
META_DESCRIPTION=D is a general-purpose programming language with static typing, systems-level access, and C-like syntax.
EXTRA_HEADERS=$(T style,
.why-d-icon {
padding-right: 0.2em;
}
.call_to_donate
{
padding: 40px 140px;
}
.call_to_donate h2
{
text-align: center;
margin: 0 0 20px 0;
}
.call_to_donate p
{
font-size: 14px;
max-width: 1000px;
margin: 20px auto;
}
.call_to_donate .btn
{
font-size: 16px;
padding: 0.6em 1em;
}
a.call_to_donate_btn
{
background-color: #B03931;
border-color: #98312A;
color: white;
}
a.call_to_donate_btn:hover
{
background-color: #742620;
border-color: #98312A;
color: white;
}
#front-testimonial
{
display: inline-block;
padding-top: 15px;
}
#front-testimonial-quote
{
padding-bottom: 10px;
font-size: 16px;
}
body#Home #content > .intro #your-code-here
{
padding-left: 1em;
position: relative;
width: 32em;
max-width: 32em;
}
body#Home #content > .intro #your-code-here .tip
{
position: absolute;
top: 0.1em;
right: 0.3em;
z-index: 1;
}
body#Home #content #tools,
body#Home #content > .intro
{
background: #F5F5F5;
}
body#Home #content > h1
{
margin: 0;
}
body#Home #content > .intro
{
padding: 1em 0 1em;
border-bottom: 1px solid #E6E6E6;
}
body#Home #content > .intro > div > div
{
display: table;
width: 100%;
}
body#Home #content > .intro > div > div > *
{
display: table-cell;
}
body#Home #content > .intro .pitch
{
font-size: large;
font-weight: 300;
margin: auto;
width: 22em;
}
body#Home #content > .intro .download
{
margin-top: 3em;
text-align: center;
}
body#Home #content > .intro .download .btn
{
margin: 0 0.3em 0.3em 0.3em;
}
body#Home #content > .intro .download .btn.action
{
border-color: #98312A;
background-color: #B03931;
color: white;
}
body#Home #content > .intro .download .btn.action:hover
{
border-color: #943029;
background-color: #742620;
color: white;
}
body#Home #content > .intro #your-code-here pre
{
margin: 0;
}
body#Home div#news
{
float: right;
width: 252px;
}
body#Home div#forum-summary
{
color: white;
width: 100%;
margin-right: 1em;
}
body#Home div#forum-summary > iframe
{
border: 0;
width: 100%;
height: 210px;
}
body#Home .more .boxes, body#Home .more .whyd
{
margin-right: 252px; /* prevent overlapping with #news */
padding-right: 4em;
}
body#Home .boxes .item .big-icon
{
padding-right: 0.5em;
}
body#Home .whyd .section
{
margin-bottom: 1.5em;
border-left: 5px solid #B03931;
border-radius: 4px;
}
body#Home .whyd .section > div
{
border: 1px solid #E6E6E6;
border-left: none;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
padding: 1.5em;
}
body#Home .whyd .section > div > *:first-child
{
margin-top: 0;
}
body#Home .whyd .section > div > *:last-child
{
margin-bottom: 0;
}
body#Home > .container
{
max-width: none;
padding: 0;
}
.org_list_header h3
{
margin: 1em 0 .5em;
}
.org_list
{
max-width: 1400px;
margin: 0 auto;
padding: 20px 0;
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-evenly;
flex-wrap: wrap;
}
.org_list a img
{
max-width: 150px;
max-height: 60px;
}
.org_list_footer
{
margin-bottom: 3em;
}
.org_list_footer a
{
display: inline-block;
padding: 0;
}
.org_list_footer a:not(:last-child)
{
padding-right: 10px;
}
body#Menu #content li.expand-container a::after
{
content: "";
}
body#Menu #content li.expand-container ul
{
display: block;
}
/* Narrow layout stage 2: forum/twitter widgets disappear from
the home page, intro pitch and your-code-here layed out vertically */
@media only screen and (max-width:54em)
{
body#Home #content > .intro > div > div
{
display: block;
width: auto;
}
body#Home #content > .intro > div > div > *
{
display: block;
}
body#Home #content > .intro .pitch
{
max-width: 22em;
width: auto;
}
body#Home #content > .intro #your-code-here
{
padding: 0;
margin: auto;
margin-top: 2em;
}
body#Home #news
{
display: none;
}
body#Home .more .boxes, body#Home .more .whyd
{
margin-right: 0;
padding-right: 0;
}