forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctod.dd
1606 lines (1226 loc) · 37.5 KB
/
ctod.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
$(COMMUNITY Programming in D for C Programmers,
$(BLOCKQUOTE William Nerdspeare,
Et tu, D? Then fall, C!
)
<img src="images/c1.gif" border=0 align=right alt="ouch!">
$(P Every experienced C programmer accumulates a series of idioms and techniques
which become second nature. Sometimes, when learning a new language, those
idioms can be so comfortable it's hard to see how to do the equivalent in the
new language. So here's a collection of common C techniques, and how to do the
corresponding task in D.
)
$(P Since C does not have object-oriented features, there's a separate section
for object-oriented issues
<a href="cpptod.html">Programming in D for C++ Programmers</a>.
)
$(P The C preprocessor is covered in
$(LINK2 pretod.html, The C Preprocessor vs D).
)
$(UL
$(LI $(LINK2 #sizeof, Getting the Size of a Type))
$(LI $(LINK2 #maxmin, Get the max and min values of a type))
$(LI $(LINK2 #types, Primitive Types))
$(LI $(LINK2 #floating, Special Floating Point Values))
$(LI $(LINK2 #modulus, Remainder after division of floating point numbers))
$(LI $(LINK2 #nans, Dealing with NANs in floating point compares))
$(LI $(LINK2 #assert, Asserts))
$(LI $(LINK2 #arrayinit, Initializing all elements of an array))
$(LI $(LINK2 #arrayloop, Looping through an array))
$(LI $(LINK2 #arraycreate, Creating an array of variable size))
$(LI $(LINK2 #strcat, String Concatenation))
$(LI $(LINK2 #printf, Formatted printing))
$(LI $(LINK2 #forwardfunc, Forward referencing functions))
$(LI $(LINK2 #funcvoid, Functions that have no arguments))
$(LI $(LINK2 #labeledbreak, Labeled break and continue statements))
$(LI $(LINK2 #goto, Goto Statements))
$(LI $(LINK2 #tagspace, Struct tag name space))
$(LI $(LINK2 #stringlookup, Looking up strings))
$(LI $(LINK2 #align, Setting struct member alignment))
$(LI $(LINK2 #anonymous, Anonymous Structs and Unions))
$(LI $(LINK2 #declaring, Declaring struct types and variables))
$(LI $(LINK2 #fieldoffset, Getting the offset of a struct member))
$(LI $(LINK2 #unioninit, Union initializations))
$(LI $(LINK2 #structinit, Struct initializations))
$(LI $(LINK2 #arrayinit2, Array initializations))
$(LI $(LINK2 #stringlit, Escaped String Literals))
$(LI $(LINK2 #ascii, Ascii vs Wide Characters))
$(LI $(LINK2 #arrayenum, Arrays that parallel an enum))
$(LI $(LINK2 #typedefs, Creating a new type with typedef))
$(LI $(LINK2 #structcmp, Comparing structs))
$(LI $(LINK2 #stringcmp, Comparing strings))
$(LI $(LINK2 #sort, Sorting arrays))
$(LI $(LINK2 #volatile, Volatile memory access))
$(LI $(LINK2 #strings, String literals))
$(LI $(LINK2 #traversal, Data Structure Traversal))
$(LI $(LINK2 #ushr, Unsigned Right Shift))
$(LI $(LINK2 #closures, Dynamic Closures))
$(LI $(LINK2 #variadic, Variadic Function Parameters))
)
<hr><!-- -------------------------------------------- -->
<h3><a name="sizeof">Getting the Size of a Type</a></h3>
<h4>The C Way</h4>
$(CCODE
sizeof(int)
sizeof(char *)
sizeof(double)
sizeof(struct Foo)
)
<h4>The D Way</h4>
<P>Use the size property:</P>
----------------------------
int.sizeof
(char *).sizeof
double.sizeof
Foo.sizeof
----------------------------
<hr><!-- ============================================ -->
<h3><a name="maxmin">Get the max and min values of a type</a></h3>
<h4>The C Way</h4>
$(CCODE
#include <limits.h>
#include <math.h>
CHAR_MAX
CHAR_MIN
ULONG_MAX
DBL_MIN
)
<h4>The D Way</h4>
----------------------------
char.max
char.min
ulong.max
double.min
----------------------------
<hr><!-- ============================================ -->
<h3><a name="types">Primitive Types</a></h3>
<h4>C to D types</h4>
$(CCODE
bool => bit
char => char
signed char => byte
unsigned char => ubyte
short => short
unsigned short => ushort
wchar_t => wchar
int => int
unsigned => uint
long => int
unsigned long => uint
long long => long
unsigned long long => ulong
float => float
double => double
long double => real
_Imaginary long double => ireal
_Complex long double => creal
)
<p>
Although char is an unsigned 8 bit type, and
wchar is an unsigned 16 bit type, they have their own separate types
in order to aid overloading and type safety.
<p>
Ints and unsigneds in C are of varying size; not so in D.
<hr><!-- ============================================ -->
<h3><a name="floating">Special Floating Point Values</a></h3>
<h4>The C Way</h4>
$(CCODE
#include <fp.h>
NAN
INFINITY
#include <float.h>
DBL_DIG
DBL_EPSILON
DBL_MANT_DIG
DBL_MAX_10_EXP
DBL_MAX_EXP
DBL_MIN_10_EXP
DBL_MIN_EXP
)
<h4>The D Way</h4>
----------------------------
double.nan
double.infinity
double.dig
double.epsilon
double.mant_dig
double.max_10_exp
double.max_exp
double.min_10_exp
double.min_exp
----------------------------
<hr><!-- ============================================ -->
<h3><a name="modulus">Remainder after division of floating point numbers</a></h3>
<h4>The C Way</h4>
$(CCODE
#include <math.h>
float f = fmodf(x,y);
double d = fmod(x,y);
long double r = fmodl(x,y);
)
<h4>The D Way</h4>
D supports the remainder ('%') operator on floating point operands:
----------------------------
float f = x % y;
double d = x % y;
real r = x % y;
----------------------------
<hr><!-- ============================================ -->
<h3><a name="nans">Dealing with NANs in floating point compares</a></h3>
<h4>The C Way</h4>
C doesn't define what happens if an operand to a compare
is NAN, and few C compilers check for it (the Digital Mars
C compiler is an exception, DM's compilers do check for NAN operands).
$(CCODE
#include <math.h>
if (isnan(x) || isnan(y))
result = FALSE;
else
result = (x < y);
)
<h4>The D Way</h4>
D offers a full complement of comparisons and operators
that work with NAN arguments.
----------------------------
result = (x < y); // false if x or y is nan
----------------------------
<hr><!-- ============================================ -->
<h3><a name="assert">Asserts are a necessary part of any good defensive coding strategy</a></h3>
<h4>The C Way</h4>
<p>
C doesn't directly support assert, but does support __FILE__
and __LINE__ from which an assert macro can be built. In fact,
there appears to be practically no other use for __FILE__ and __LINE__.
$(CCODE
#include <assert.h>
assert(e == 0);
)
<h4>The D Way</h4>
D simply builds assert into the language:
----------------------------
assert(e == 0);
----------------------------
<hr><!-- ============================================ -->
<h3><a name="arrayinit">Initializing all elements of an array</a></h3>
<h4>The C Way</h4>
$(CCODE
#define ARRAY_LENGTH 17
int array[ARRAY_LENGTH];
for (i = 0; i < ARRAY_LENGTH; i++)
array[i] = value;
)
<h4>The D Way</h4>
----------------------------
int array[17];
array[] = value;
----------------------------
<hr><!-- ============================================ -->
<h3><a name="arrayloop">Looping through an array</a></h3>
<h4>The C Way</h4>
<p>
The array length is defined separately, or a clumsy
sizeof() expression is used to get the length.
$(CCODE
#define ARRAY_LENGTH 17
int array[ARRAY_LENGTH];
for (i = 0; i < ARRAY_LENGTH; i++)
func(array[i]);
)
or:
$(CCODE
int array[17];
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++)
func(array[i]);
)
<h4>The D Way</h4>
The length of an array is accessible through the property "length".
----------------------------
int array[17];
for (i = 0; i < array.length; i++)
func(array[i]);
----------------------------
or even better:
----------------------------
int array[17];
foreach (int value; array)
func(value);
----------------------------
<hr><!-- ============================================ -->
<h3><a name="arraycreate">Creating an array of variable size</a></h3>
<h4>The C Way</h4>
C cannot do this with arrays. It is necessary to create a separate
variable for the length, and then explicitly manage the size of
the array:
$(CCODE
#include <stdlib.h>
int array_length;
int *array;
int *newarray;
newarray = (int *)
realloc(array, (array_length + 1) * sizeof(int));
if (!newarray)
error("out of memory");
array = newarray;
array[array_length++] = x;
)
<h4>The D Way</h4>
D supports dynamic arrays, which can be easily resized. D supports
all the requisite memory management.
----------------------------
int[] array;
array.length = array.length + 1;
array[array.length - 1] = x;
----------------------------
<hr><!-- ============================================ -->
<h3><a name="strcat">String Concatenation</a></h3>
<h4>The C Way</h4>
There are several difficulties to be resolved, like
when can storage be freed, dealing with null pointers,
finding the length of the strings, and memory allocation:
$(CCODE
#include <string.h>
char *s1;
char *s2;
char *s;
// Concatenate s1 and s2, and put result in s
free(s);
s = (char *)malloc((s1 ? strlen(s1) : 0) +
(s2 ? strlen(s2) : 0) + 1);
if (!s)
error("out of memory");
if (s1)
strcpy(s, s1);
else
*s = 0;
if (s2)
strcpy(s + strlen(s), s2);
// Append "hello" to s
char hello[] = "hello";
char *news;
size_t lens = s ? strlen(s) : 0;
news = (char *)
realloc(s, (lens + sizeof(hello) + 1) * sizeof(char));
if (!news)
error("out of memory");
s = news;
memcpy(s + lens, hello, sizeof(hello));
)
<h4>The D Way</h4>
D overloads the operators ~ and ~= for char and wchar arrays to mean
concatenate and append, respectively:
----------------------------
char[] s1;
char[] s2;
char[] s;
s = s1 ~ s2;
s ~= "hello";
----------------------------
<hr><!-- ============================================ -->
<h3><a name="printf">Formatted printing</a></h3>
<h4>The C Way</h4>
printf() is the general purpose formatted print routine:
$(CCODE
#include <stdio.h>
printf("Calling all cars %d times!\n", ntimes);
)
<h4>The D Way</h4>
What can we say? printf() rules:
----------------------------
printf("Calling all cars %d times!\n", ntimes);
----------------------------
writefln() improves on printf() by being type-aware and type-safe:
-----------------------
import std.stdio;
writefln("Calling all cars %s times!", ntimes);
-----------------------
<hr><!-- ============================================ -->
<h3><a name="forwardfunc">Forward referencing functions</a></h3>
<h4>The C Way</h4>
Functions cannot be forward referenced. Hence, to call a function
not yet encountered in the source file, it is necessary to insert
a function declaration lexically preceding the call.
$(CCODE
void forwardfunc();
void myfunc()
{
forwardfunc();
}
void forwardfunc()
{
...
}
)
<h4>The D Way</h4>
The program is looked at as a whole, and so not only is it not
necessary to code forward declarations, it is not even allowed!
D avoids the tedium and errors associated with writing forward
referenced function declarations twice.
Functions can be defined in any order.
----------------------------
void myfunc()
{
forwardfunc();
}
void forwardfunc()
{
...
}
----------------------------
<hr><!-- ============================================ -->
<h3><a name="funcvoid">Functions that have no arguments</a></h3>
<h4>The C Way</h4>
$(CCODE
void function(void);
)
<h4>The D Way</h4>
D is a strongly typed language, so there is no need to explicitly
say a function takes no arguments, just don't declare it has having
arguments.
----------------------------
void function()
{
...
}
----------------------------
<hr><!-- ============================================ -->
<h3><a name="labeledbreak">Labeled break and continue statements</a></h3>
<h4>The C Way</h4>
Break and continue statements only apply to the innermost nested loop or
switch, so a multilevel break must use a goto:
$(CCODE
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (j == 3)
goto Louter;
if (j == 4)
goto L2;
}
L2:
;
}
Louter:
;
)
<h4>The D Way</h4>
Break and continue statements can be followed by a label. The label
is the label for an enclosing loop or switch, and the break applies
to that loop.
----------------------------
Louter:
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (j == 3)
break Louter;
if (j == 4)
continue Louter;
}
}
// break Louter goes here
----------------------------
<hr><!-- ============================================ -->
<h3><a name="goto">Goto Statements</a></h3>
<h4>The C Way</h4>
The much maligned goto statement is a staple for professional C coders.
It's
necessary to make up for sometimes inadequate control flow statements.
<h4>The D Way</h4>
Many C-way goto statements can be eliminated with the D feature of
labeled
break and continue statements. But D is a practical language for
practical
programmers who know when the rules need to be broken. So of course D
supports goto statements.
<hr><!-- ============================================ -->
<h3><a name="tagspace">Struct tag name space</a></h3>
<h4>The C Way</h4>
It's annoying to have to put the struct keyword every time a type is specified,
so a common idiom is to use:
$(CCODE
typedef struct ABC { ... } ABC;
)
<h4>The D Way</h4>
Struct tag names are not in a separate name space, they are in the same name
space as ordinary names. Hence:
----------------------------
struct ABC { ... }
----------------------------
<hr><!-- ============================================ -->
<h3><a name="stringlookup">Looking up strings</a></h3>
<h4>The C Way</h4>
Given a string, compare the string against a list of possible
values and take action based on which one it is. A typical use
for this might be command line argument processing.
$(CCODE
#include <string.h>
void dostring(char *s)
{
enum Strings { Hello, Goodbye, Maybe, Max };
static char *table[] = { "hello", "goodbye", "maybe" };
int i;
for (i = 0; i < Max; i++)
{
if (strcmp(s, table[i]) == 0)
break;
}
switch (i)
{
case Hello: ...
case Goodbye: ...
case Maybe: ...
default: ...
}
}
)
The problem with this is trying to maintain 3 parallel data
structures, the enum, the table, and the switch cases. If there
are a lot of values, the connection between the 3 may not be so
obvious when doing maintenance, and so the situation is ripe for
bugs.
Additionally, if the number of values becomes large, a binary or
hash lookup will yield a considerable performance increase over
a simple linear search. But coding these can be time consuming,
and they need to be debugged. It's typical that such just never
gets done.
<h4>The D Way</h4>
D extends the concept of switch statements to be able to handle
strings as well as numbers. Then, the way to code the string
lookup becomes straightforward:
----------------------------
void dostring(char[] s)
{
switch (s)
{
case "hello": ...
case "goodbye": ...
case "maybe": ...
default: ...
}
}
----------------------------
Adding new cases becomes easy. The compiler can be relied on
to generate a fast lookup scheme for it, eliminating the bugs
and time required in hand-coding one.
<hr><!-- ============================================ -->
<h3><a name="align">Setting struct member alignment</a></h3>
<h4>The C Way</h4>
It's done through a command line switch which affects the entire
program, and woe results if any modules or libraries didn't get
recompiled. To address this, $(TT #pragma)s are used:
$(CCODE
#pragma pack(1)
struct ABC
{
...
};
#pragma pack()
)
But $(TT #pragma)s are nonportable both in theory and in practice from
compiler to compiler.
<h4>The D Way</h4>
$(P D has a syntax for setting the alignment that is common
to all D compilers. The actual alignment done is compatible
with the companion C compiler's alignment, for ABI compatibility.
To match a particular layout across architectures, use
$(TT align(1)) and manually specify it.
)
----------------------------
struct ABC
{
int z; // z is aligned to the default
align (1) int x; // x is byte aligned
align (4)
{
... // declarations in {} are dword aligned
}
align (2): // switch to word alignment from here on
int y; // y is word aligned
}
----------------------------
<hr><!-- ============================================ -->
<h3><a name="anonymous">Anonymous Structs and Unions</a></h3>
Sometimes, it's nice to control the layout of a struct with nested structs and unions.
<h4>The C Way</h4>
C doesn't allow anonymous structs or unions, which means that dummy tag names
and dummy members are necessary:
$(CCODE
struct Foo
{
int i;
union Bar
{
struct Abc { int x; long y; } _abc;
char *p;
} _bar;
};
#define x _bar._abc.x
#define y _bar._abc.y
#define p _bar.p
struct Foo f;
f.i;
f.x;
f.y;
f.p;
)
Not only is it clumsy, but using macros means a symbolic debugger won't understand
what is being done, and the macros have global scope instead of struct scope.
<h4>The D Way</h4>
Anonymous structs and unions are used to control the layout in a
more natural manner:
----------------------------
struct Foo
{
int i;
union
{
struct { int x; long y; }
char* p;
}
}
Foo f;
f.i;
f.x;
f.y;
f.p;
----------------------------
<hr><!-- ============================================ -->
<h3><a name="declaring">Declaring struct types and variables</a></h3>
<h4>The C Way</h4>
$(P Is to do it in one statement ending with a semicolon:)
$(CCODE
struct Foo { int x; int y; } foo;
)
$(P Or to separate the two:)
$(CCODE
struct Foo { int x; int y; }; // note terminating ;
struct Foo foo;
)
<h4>The D Way</h4>
$(P Struct definitions and declarations can't be done in the same
statement:
)
----------------------------
struct Foo { int x; int y; } // note there is no terminating ;
Foo foo;
----------------------------
$(P which means that the terminating ; can be dispensed with,
eliminating the confusing difference between struct {} and function
block {} in how semicolons are used.
)
<hr><!-- ============================================ -->
<h3><a name="fieldoffset">Getting the offset of a struct member</a></h3>
<h4>The C Way</h4>
Naturally, another macro is used:
$(CCODE
#include <stddef>
struct Foo { int x; int y; };
off = offsetof(Foo, y);
)
<h4>The D Way</h4>
An offset is just another property:
----------------------------
struct Foo { int x; int y; }
off = Foo.y.offsetof;
----------------------------
<hr><!-- ============================================ -->
<h3><a name="unioninit">Union Initializations</a></h3>
<h4>The C Way</h4>
Unions are initialized using the "first member" rule:
$(CCODE
union U { int a; long b; };
union U x = { 5 }; // initialize member 'a' to 5
)
Adding union members or rearranging them can have disastrous consequences
for any initializers.
<h4>The D Way</h4>
In D, which member is being initialized is mentioned explicitly:
----------------------------
union U { int a; long b; }
U x = { a:5 };
----------------------------
avoiding the confusion and maintenance problems.
<hr><!-- ============================================ -->
<h3><a name="structinit">Struct Initializations</a></h3>
<h4>The C Way</h4>
Members are initialized by their position within the { }s:
$(CCODE
struct S { int a; int b; };
struct S x = { 5, 3 };
)
This isn't much of a problem with small structs, but when there
are numerous members, it becomes tedious to get the initializers
carefully lined up with the field declarations. Then, if members are
added or rearranged, all the initializations have to be found and
modified appropriately. This is a minefield for bugs.
<h4>The D Way</h4>
Member initialization can be done explicitly:
----------------------------
struct S { int a; int b; }
S x = { b:3, a:5 };
----------------------------
The meaning is clear, and there no longer is a positional dependence.
<hr><!-- ============================================ -->
<h3><a name="arrayinit2">Array Initializations</a></h3>
<h4>The C Way</h4>
C initializes array by positional dependence:
$(CCODE
int a[3] = { 3,2,2 };
)
Nested arrays may or may not have the { }:
$(CCODE
int b[3][2] = { 2,3, {6,5}, 3,4 };
)
<h4>The D Way</h4>
D does it by positional dependence too, but an index can be used as well.
The following all produce the same result:
----------------------------
int[3] a = [ 3, 2, 0 ];
int[3] a = [ 3, 2 ]; // unsupplied initializers are 0, just like in C
int[3] a = [ 2:0, 0:3, 1:2 ];
int[3] a = [ 2:0, 0:3, 2 ]; // if not supplied, the index is the
// previous one plus one.
----------------------------
This can be handy if the array will be indexed by an enum, and the order of
enums may be changed or added to:
----------------------------
enum color { black, red, green }
int[3] c = [ black:3, green:2, red:5 ];
----------------------------
Nested array initializations must be explicit:
----------------------------
int[2][3] b = [ [2,3], [6,5], [3,4] ];
int[2][3] b = [[2,6,3],[3,5,4]]; // error
----------------------------
<hr><!-- ============================================ -->
<h3><a name="stringlit">Escaped String Literals</a></h3>
<h4>The C Way</h4>
C has problems with the DOS file system because a \ is an escape in a string. To specifiy file c:\root\file.c:
$(CCODE
char file[] = "c:\\root\\file.c";
)
This gets even more unpleasant with regular expressions.
Consider the escape sequence to match a quoted string:
$(CCODE
/"[^\\]*(\\.[^\\]*)*"/
)
<P>In C, this horror is expressed as:
$(CCODE
char quoteString[] = "\"[^\\\\]*(\\\\.[^\\\\]*)*\"";
)
<h4>The D Way</h4>
Within strings, it is WYSIWYG (what you see is what you get).
Escapes are in separate strings. So:
----------------------------
char[] file = `c:\root\file.c`;
char[] quoteString = \" r"[^\\]*(\\.[^\\]*)*" \";
----------------------------
The famous hello world string becomes:
----------------------------
char[] hello = "hello world" \n;
----------------------------
<hr><!-- ============================================ -->
<h3><a name="ascii">Ascii vs Wide Characters</a></h3>
<P>Modern programming requires that wchar strings be supported in an easy way, for internationalization of the programs.
<h4>The C Way</h4>
C uses the wchar_t and the L prefix on strings:
$(CCODE
#include <wchar.h>
char foo_ascii[] = "hello";
wchar_t foo_wchar[] = L"hello";
)
Things get worse if code is written to be both ascii and wchar compatible.
A macro is used to switch strings from ascii to wchar:
$(CCODE
#include <tchar.h>
tchar string[] = TEXT("hello");
)
<h4>The D Way</h4>
The type of a string is determined by semantic analysis, so there is no need to wrap strings in a macro call:
-----------------------------
char[] foo_ascii = "hello"; // string is taken to be ascii
wchar[] foo_wchar = "hello"; // string is taken to be wchar
-----------------------------
<hr><!-- ============================================ -->
<h3><a name="arrayenum">Arrays that parallel an enum</a></h3>
<h4>The C Way</h4>
Consider:
$(CCODE
enum COLORS { red, blue, green, max };
char *cstring[max] = {"red", "blue", "green" };
)
This is fairly easy to get right because the number of entries is small. But suppose it gets to be fairly large. Then it can get difficult to maintain correctly when new entries are added.
<h4>The D Way</h4>
-----------------------------
enum COLORS { red, blue, green }
char[][COLORS.max + 1] cstring =
[
COLORS.red : "red",
COLORS.blue : "blue",
COLORS.green : "green",
];
-----------------------------
Not perfect, but better.
<hr><!-- ============================================ -->
<h3><a name="typedefs">Creating a new type with typedef</a></h3>