forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.dd
1932 lines (1597 loc) · 49.9 KB
/
expression.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
$(SPEC_S Expressions,
$(P C and C++ programmers will find the D expressions very familiar,
with a few interesting additions.
)
$(P Expressions are used to compute values with a resulting type.
These values can then be assigned,
tested, or ignored. Expressions can also have side effects.
)
<h2><a name="order-of-evaluation">Order Of Evaluation</a></h2>
$(P The following binary expressions are evaluated in strictly
left-to-right order:)
$(P
$(V2
$(GLINK OrExpression),
$(GLINK XorExpression),
$(GLINK AndExpression),
$(GLINK CmpExpression),
$(GLINK ShiftExpression),
$(GLINK AddExpression),
$(GLINK CatExpression),
$(GLINK MulExpression),
$(GLINK PowExpression),
)
$(GLINK CommaExpression),
$(GLINK OrOrExpression),
$(GLINK AndAndExpression)
)
$(P The following binary expressions are evaluated in an
implementation-defined order:)
$(P
$(GLINK AssignExpression),
$(V1
$(GLINK OrExpression),
$(GLINK XorExpression),
$(GLINK AndExpression),
$(GLINK CmpExpression),
$(GLINK ShiftExpression),
$(GLINK AddExpression),
$(GLINK CatExpression),
$(GLINK MulExpression),
)
function parameters
)
$(P It is an error
to depend on order of evaluation when it is not specified.
For example, the following are illegal:
)
-------------
i = i++;
c = a + (a = b);
func(++i, ++i);
-------------
$(P If the compiler can determine that the result of an expression
is illegally dependent on the order of evaluation, it can issue
an error (but is not required to). The ability to detect these kinds
of errors is a quality of implementation issue.
)
<h2><a name="Expression">Expressions</a></h2>
$(GRAMMAR
$(GNAME Expression):
$(I CommaExpression)
$(GNAME CommaExpression):
$(GLINK AssignExpression)
$(GLINK AssignExpression) $(B ,) $(I CommaExpression)
)
The left operand of the $(B ,) is evaluated, then the right operand
is evaluated. The type of the expression is the type of the right
operand, and the result is the result of the right operand.
<h2>Assign Expressions</h2>
$(GRAMMAR
$(GNAME AssignExpression):
$(GLINK ConditionalExpression)
$(GLINK ConditionalExpression) $(B =) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B +=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B -=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B *=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B /=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B %=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B &=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B |=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B ^=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B ~=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B <<=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B >>=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(B >>>=) $(I AssignExpression)
$(V2
$(GLINK ConditionalExpression) $(B ^^=) $(I AssignExpression))
)
The right operand is implicitly converted to the type of the
left operand, and assigned to it. The result type is the type
of the lvalue, and the result value is the value of the lvalue
after the assignment.
<p>
The left operand must be an lvalue.
<h3>Assignment Operator Expressions</h3>
Assignment operator expressions, such as:
--------------
$(I a op= b)
--------------
are semantically equivalent to:
--------------
$(I a = a op b)
--------------
except that operand $(I a) is only evaluated once.
<h2>Conditional Expressions</h2>
$(GRAMMAR
$(GNAME ConditionalExpression):
$(GLINK OrOrExpression)
$(GLINK OrOrExpression) $(B ?) $(GLINK Expression) $(B :) $(I ConditionalExpression)
)
The first expression is converted to bool, and is evaluated.
If it is true, then the second expression is evaluated, and
its result is the result of the conditional expression.
If it is false, then the third expression is evaluated, and
its result is the result of the conditional expression.
If either the second or third expressions are of type void,
then the resulting type is void. Otherwise, the second and third
expressions are implicitly converted to a common type which becomes
the result type of the conditional expression.
<h2>OrOr Expressions</h2>
$(GRAMMAR
$(GNAME OrOrExpression):
$(GLINK AndAndExpression)
$(I OrOrExpression) $(B ||) $(GLINK AndAndExpression)
)
The result type of an $(I OrOrExpression) is bool,
unless the right operand
has type void, when the result is type void.
<p>
The $(I OrOrExpression) evaluates its left operand.
If the left operand, converted to type bool, evaluates to
true, then the right operand is not evaluated. If the result type of
the $(I OrOrExpression) is bool then the result of the
expression is true.
If the left operand is false, then the right
operand is evaluated.
If the result type of
the $(I OrOrExpression) is bool then the result of the
expression is the right operand converted to type bool.
<h2>AndAnd Expressions</h2>
$(V1
$(GRAMMAR
$(GNAME AndAndExpression):
$(GLINK OrExpression)
$(I AndAndExpression) $(B &&) $(GLINK OrExpression)
)
)
$(V2
$(GRAMMAR
$(GNAME AndAndExpression):
$(GLINK OrExpression)
$(I AndAndExpression) $(B &&) $(GLINK OrExpression)
$(GLINK CmpExpression)
$(I AndAndExpression) $(B &&) $(GLINK CmpExpression)
)
)
$(P The result type of an $(I AndAndExpression) is bool, unless the right operand
has type void, when the result is type void.
)
$(P The $(I AndAndExpression) evaluates its left operand.
)
$(P If the left operand, converted to type bool, evaluates to
false, then the right operand is not evaluated. If the result type of
the $(I AndAndExpression) is bool then the result of the
expression is false.
)
$(P If the left operand is true, then the right
operand is evaluated.
If the result type of
the $(I AndAndExpression) is bool then the result of the
expression is the right operand converted to type bool.
)
<h2>Bitwise Expressions</h2>
Bit wise expressions perform a bitwise operation on their operands.
Their operands must be integral types.
First, the default integral promotions are done. Then, the bitwise
operation is done.
<h3>Or Expressions</h3>
$(GRAMMAR
$(GNAME OrExpression):
$(GLINK XorExpression)
$(I OrExpression) $(B |) $(GLINK XorExpression)
)
The operands are OR'd together.
<h3>Xor Expressions</h3>
$(GRAMMAR
$(GNAME XorExpression):
$(GLINK AndExpression)
$(I XorExpression) $(B ^) $(GLINK AndExpression)
)
The operands are XOR'd together.
<h3>And Expressions</h3>
$(V1
$(GRAMMAR
$(GNAME AndExpression):
$(GLINK CmpExpression)
$(I AndExpression) $(B &) $(GLINK CmpExpression)
)
)
$(V2
$(GRAMMAR
$(GNAME AndExpression):
$(GLINK ShiftExpression)
$(I AndExpression) $(B &) $(GLINK ShiftExpression)
)
)
The operands are AND'd together.
<h2><a name="CmpExpression">Compare Expressions</a></h2>
$(GRAMMAR
$(GNAME CmpExpression):
$(GLINK ShiftExpression)
$(GLINK EqualExpression)
$(GLINK IdentityExpression)
$(GLINK RelExpression)
$(GLINK InExpression)
)
<h2><a name="EqualExpression">Equality Expressions</a></h2>
$(GRAMMAR
$(GNAME EqualExpression):
$(GLINK ShiftExpression) $(B ==) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !=) $(GLINK ShiftExpression)
)
Equality expressions compare the two operands for equality ($(B ==))
or inequality ($(B !=)).
The type of the result is bool. The operands
go through the usual conversions to bring them to a common type before
comparison.
<p>
If they are integral values or pointers, equality
is defined as the bit pattern of the type matches exactly.
Equality for struct objects means the bit patterns of the objects
match exactly (the existence of alignment holes in the objects
is accounted for, usually by setting them all to 0 upon
initialization).
Equality for floating point types is more complicated. -0 and
+0 compare as equal. If either or both operands are NAN, then
both the == returns false and != returns true. Otherwise, the bit
patterns are compared for equality.
<p>
For complex numbers, equality is defined as equivalent to:
<pre>
x.re == y.re && x.im == y.im
</pre>
and inequality is defined as equivalent to:
<pre>
x.re != y.re || x.im != y.im
</pre>
$(P For class and struct objects, the expression $(TT (a == b))
is rewritten as
$(TT a.opEquals(b)), and $(TT (a != b)) is rewritten as
$(TT !a.opEquals(b)).
)
$(P For class objects, the $(CODE ==) and $(CODE !=)
operators compare the
contents of the objects. Therefore, comparing against
$(CODE null) is invalid, as $(CODE null) has no contents.
Use the $(CODE is) and $(CODE !is) operators instead.
)
---
class C;
C c;
if (c == null) // error
...
if (c is null) // ok
...
---
$(P For static and dynamic arrays, equality is defined as the
lengths of the arrays
matching, and all the elements are equal.
)
<h3><a name="IdentityExpression">Identity Expressions</a></h3>
$(GRAMMAR
$(GNAME IdentityExpression):
$(GLINK ShiftExpression) $(B is) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !is) $(GLINK ShiftExpression)
)
$(P The $(B is) compares for identity.
To compare for not identity, use $(TT $(I e1) $(B !is) $(I e2)).
The type of the result is bool. The operands
go through the usual conversions to bring them to a common type before
comparison.
)
$(P For class objects, identity is defined as the object references
are for the same object. Null class objects can be compared with
$(B is).
)
$(P For struct objects, identity is defined as the bits in the
struct being identical.
)
$(P For static and dynamic arrays, identity is defined as referring
to the same array elements and the same number of elements.
)
$(P For other operand types, identity is defined as being the same
as equality.
)
$(P The identity operator $(B is) cannot be overloaded.
)
<h2>Relational Expressions</h2>
$(GRAMMAR
$(GNAME RelExpression):
$(GLINK ShiftExpression) $(B <) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B <=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B >) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B >=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !<>=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !<>) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B <>) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B <>=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !>) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !>=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !<) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !<=) $(GLINK ShiftExpression)
)
First, the integral promotions are done on the operands.
The result type of a relational expression is bool.
<p>
For class objects, the result of Object.opCmp() forms the left
operand, and 0 forms the right operand. The result of the
relational expression (o1 op o2) is:
<pre>
(o1.opCmp(o2) op 0)
</pre>
It is an error to compare objects if one is $(B null).
<p>
For static and dynamic arrays, the result of the relational
op is the result of the operator applied to the first non-equal
element of the array. If two arrays compare equal, but are of
different lengths, the shorter array compares as "less" than the
longer array.
<h3>Integer comparisons</h3>
$(P Integer comparisons happen when both operands are integral
types.
)
$(TABLE1
<caption>Integer comparison operators</caption>
$(TR
$(TH Operator)$(TH Relation)
)$(TR
$(TD <) $(TD less)
)$(TR
$(TD >) $(TD greater)
)$(TR
$(TD <=) $(TD less or equal)
)$(TR
$(TD >=) $(TD greater or equal)
)$(TR
$(TD ==) $(TD equal)
)$(TR
$(TD !=) $(TD not equal)
)
)
$(P It is an error to have one operand be signed and the other
unsigned for a <, <=, > or >= expression.
Use casts to make both operands signed or both operands unsigned.
)
<h3><a name="floating_point_comparisons">Floating point comparisons</a></h3>
If one or both operands are floating point, then a floating
point comparison is performed.
<p>
Useful floating point operations must take into account NAN values.
In particular, a relational operator can have NAN operands.
The result of a relational operation on float
values is less, greater, equal, or unordered (unordered means
either or both of the
operands is a NAN). That means there are 14 possible comparison
conditions to test for:
<p>
$(TABLE1
<caption>Floating point comparison operators</caption>
$(TR
$(TH Operator)
$(TH Greater Than)
$(TH Less Than)
$(TH Equal)
$(TH Unordered)
$(TH Exception)
$(TH Relation)
)
$(TR
$(TD ==) $(TD F)$(TD F)$(TD T)$(TD F)$(TD no) $(TD equal)
)
$(TR
$(TD !=) $(TD T)$(TD T)$(TD F)$(TD T)$(TD no) $(TD unordered, less, or greater)
)
$(TR
$(TD >) $(TD T)$(TD F)$(TD F)$(TD F)$(TD yes) $(TD greater)
)
$(TR
$(TD >=) $(TD T)$(TD F)$(TD T)$(TD F)$(TD yes) $(TD greater or equal)
)
$(TR
$(TD <) $(TD F)$(TD T)$(TD F)$(TD F)$(TD yes) $(TD less)
)
$(TR
$(TD <=) $(TD F)$(TD T)$(TD T)$(TD F)$(TD yes) $(TD less or equal)
)
$(TR
$(TD !<>=) $(TD F)$(TD F)$(TD F)$(TD T)$(TD no) $(TD unordered)
)
$(TR
$(TD <>) $(TD T)$(TD T)$(TD F)$(TD F)$(TD yes) $(TD less or greater)
)
$(TR
$(TD <>=) $(TD T)$(TD T)$(TD T)$(TD F)$(TD yes) $(TD less, equal, or greater)
)
$(TR
$(TD !<=) $(TD T)$(TD F)$(TD F)$(TD T)$(TD no) $(TD unordered or greater)
)
$(TR
$(TD !<) $(TD T)$(TD F)$(TD T)$(TD T)$(TD no) $(TD unordered, greater, or equal)
)
$(TR
$(TD !>=) $(TD F)$(TD T)$(TD F)$(TD T)$(TD no) $(TD unordered or less)
)
$(TR
$(TD !>) $(TD F)$(TD T)$(TD T)$(TD T)$(TD no) $(TD unordered, less, or equal)
)
$(TR
$(TD !<>) $(TD F)$(TD F)$(TD T)$(TD T)$(TD no) $(TD unordered or equal)
)
)
<h4>Notes:</h4>
$(OL
$(LI For floating point comparison operators,
$(CODE ($(I a) !$(I op) $(I b)))
is not the same as $(CODE !($(I a op b))).)
$(LI "Unordered" means one or both of the operands is a NAN.)
$(LI "Exception" means the $(I Invalid Exception) is raised if one
of the operands is a NAN. It does not mean an exception
is thrown. The $(I Invalid Exception) can be checked
using the functions in $(LINK2 phobos/std_c_fenv.html, std.c.fenv).
)
)
<h3><a name="class_comparisons">Class comparisons</a></h3>
$(P For class objects, the relational
operators compare the
contents of the objects. Therefore, comparing against
$(CODE null) is invalid, as $(CODE null) has no contents.
)
---
class C;
C c;
if (c < null) // error
...
---
<h2><a name="InExpression">In Expressions</a></h2>
$(GRAMMAR
$(GNAME InExpression):
$(GLINK ShiftExpression) $(B in) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(B !in) $(GLINK ShiftExpression)
)
$(P An associative array can be tested to see if an element is in the array:
)
-------------
int foo[char[]];
...
if ("hello" in foo)
...
-------------
$(P The $(B in) expression has the same precedence as the
relational expressions $(B <), $(B <=),
etc.
The return value of the $(I InExpression) is $(B null)
if the element is not in the array;
if it is in the array it is a pointer to the element.
)
$(P The $(B !in) expression is the logical negation of the $(B in)
operation.
)
<h2>Shift Expressions</h2>
$(GRAMMAR
$(GNAME ShiftExpression):
$(GLINK AddExpression)
$(I ShiftExpression) $(B <<) $(GLINK AddExpression)
$(I ShiftExpression) $(B >>) $(GLINK AddExpression)
$(I ShiftExpression) $(B >>>) $(GLINK AddExpression)
)
The operands must be integral types, and undergo the usual integral
promotions. The result type is the type of the left operand after
the promotions. The result value is the result of shifting the bits
by the right operand's value.
<p>
$(B <<) is a left shift.
$(B >>) is a signed right shift.
$(B >>>) is an unsigned right shift.
<p>
It's illegal to shift by more bits than the size of the
quantity being shifted:
-------------
int c;
c << 33; // error
-------------
<h2>Add Expressions</h2>
$(GRAMMAR
$(GNAME AddExpression):
$(GLINK MulExpression)
$(I AddExpression) $(B +) $(GLINK MulExpression)
$(I AddExpression) $(B -) $(GLINK MulExpression)
$(GLINK CatExpression)
)
$(P If the operands are of integral types, they undergo integral
promotions, and then are brought to a common type using the
usual arithmetic conversions.
)
$(P If either operand is a floating point type, the other is implicitly
converted to floating point and they are brought to a common type
via the usual arithmetic conversions.
)
$(P If the operator is $(B +) or $(B -), and
the first operand is a pointer, and the second is an integral type,
the resulting type is the type of the first operand, and the resulting
value is the pointer plus (or minus) the second operand multiplied by
the size of the type pointed to by the first operand.
)
$(P If the second operand is a pointer, and the first is an integral type,
and the operator is $(B +),
the operands are reversed and the pointer arithmetic just described
is applied.
)
$(P If both operands are pointers, and the operator is $(B +),
then it is illegal. For $(B -), the pointers are subtracted and the
result is divided by the size of the type pointed to by the
operands. It is an error if the pointers point to different types.
)
$(P Add expressions for floating point operands are not associative.
)
<h2>Cat Expressions</h2>
$(GRAMMAR
$(GNAME CatExpression):
$(I AddExpression) $(B ~) $(GLINK MulExpression)
)
$(P A $(I CatExpression) concatenates arrays, producing
a dynmaic array with the result. The arrays must be
arrays of the same element type. If one operand is an array
and the other is of that array's element type, that element
is converted to an array of length 1 of that element,
and then the concatenation is performed.
)
<h2>Mul Expressions</h2>
$(GRAMMAR
$(GNAME MulExpression):
$(GLINK UnaryExpression)
$(I MulExpression) $(B *) $(GLINK UnaryExpression)
$(I MulExpression) $(B /) $(GLINK UnaryExpression)
$(I MulExpression) $(B %) $(GLINK UnaryExpression)
)
$(P The operands must be arithmetic types. They undergo integral
promotions, and then are brought to a common type using the
usual arithmetic conversions.
)
$(P For integral operands, the $(B *), $(B /), and $(B %)
correspond to multiply, divide, and modulus operations.
For multiply, overflows are ignored and simply chopped to fit
into the integral type.
)
$(P For integral operands of the $(B /) and $(B %) operators,
the quotient rounds towards zero and the remainder has the
same sign as the dividend.
If the divisor is zero, an Exception is thrown.
)
$(P For floating point operands, the * and / operations correspond
to the IEEE 754 floating point equivalents. % is not the same as
the IEEE 754 remainder. For example, 15.0 % 10.0 == 5.0, whereas
for IEEE 754, remainder(15.0,10.0) == -5.0.
)
$(P Mul expressions for floating point operands are not associative.
)
<h2><a name="UnaryExpression">Unary Expressions</a></h2>
$(GRAMMAR
$(GNAME UnaryExpression):
$(V1 $(GLINK PostfixExpression))$(V2 $(GLINK PowExpression))
$(B &) $(I UnaryExpression)
$(B ++) $(I UnaryExpression)
$(B --) $(I UnaryExpression)
$(B *) $(I UnaryExpression)
$(B -) $(I UnaryExpression)
$(B +) $(I UnaryExpression)
$(B !) $(I UnaryExpression)
$(B ~) $(I UnaryExpression)
$(B $(LPAREN)) $(GLINK2 declaration, Type) $(B $(RPAREN) .) $(I Identifier)
$(GLINK NewExpression)
$(GLINK DeleteExpression)
$(GLINK CastExpression)
)
<h3>New Expressions</h3>
$(GRAMMAR
$(GNAME NewExpression):
$(B new) $(I AllocatorArguments)$(OPT) $(LINK2 declaration.html#Type, $(I Type)) $(B [) $(GLINK AssignExpression) $(B ])
$(B new) $(I AllocatorArguments)$(OPT) $(LINK2 declaration.html#Type, $(I Type)) $(B $(LPAREN)) $(GLINK ArgumentList) $(B $(RPAREN))
$(B new) $(I AllocatorArguments)$(OPT) $(LINK2 declaration.html#Type, $(I Type))
$(LINK2 class.html#anonymous, $(I NewAnonClassExpression))
$(GNAME AllocatorArguments):
$(B $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(B $(RPAREN))
$(GNAME ArgumentList):
$(GLINK AssignExpression)
$(V2 $(GLINK AssignExpression) $(B ,)
) $(GLINK AssignExpression) $(B ,) $(I ArgumentList)
)
$(P $(I NewExpression)s are used to allocate memory on the garbage
collected heap (default) or using a class or struct specific allocator.
)
$(P To allocate multidimensional arrays, the declaration reads
in the same order as the prefix array declaration order.
)
-------------
char[][] foo; // dynamic array of strings
...
foo = new char[][30]; // allocate array of 30 strings
-------------
$(P The above allocation can also be written as:)
-------------
foo = new char[][](30); // allocate array of 30 strings
-------------
$(P To allocate the nested arrays, multiple arguments can be used:)
---------------
int[][][] bar;
...
bar = new int[][][](5,20,30);
---------------
$(P Which is equivalent to:)
----------
bar = new int[][][5];
foreach (ref a; bar)
{
a = new int[][20];
foreach (ref b; a)
{
b = new int[30];
}
}
-----------
$(P If there is a $(B new $(LPAREN)) $(GLINK ArgumentList) $(B $(RPAREN)),
then
those arguments are passed to the class or struct specific allocator
function after the size argument.
)
$(P If a $(I NewExpression) is used as an initializer for
a function local variable with $(B scope) storage class,
and the $(GLINK ArgumentList) to $(B new) is empty, then
the instance is allocated on the stack rather than the heap
or using the class specific allocator.
)
<h3>Delete Expressions</h3>
$(GRAMMAR
$(GNAME DeleteExpression):
$(B delete) $(GLINK UnaryExpression)
)
$(P If the $(I UnaryExpression) is a class object reference, and
there is a destructor for that class, the destructor
is called for that object instance.
)
$(P Next, if the $(I UnaryExpression) is a class object reference, or
a pointer to a struct instance, and the class or struct
has overloaded operator delete, then that operator delete is called
for that class object instance or struct instance.
)
$(P Otherwise, the garbage collector is called to immediately free the
memory allocated for the class instance or struct instance.
If the garbage collector was not used to allocate the memory for
the instance, undefined behavior will result.
)
$(P If the $(I UnaryExpression) is a pointer or a dynamic array,
the garbage collector is called to immediately release the
memory.
If the garbage collector was not used to allocate the memory for
the instance, undefined behavior will result.
)
$(P The pointer, dynamic array, or reference is set to $(B null)
after the delete is performed.
)
$(P If $(I UnaryExpression) is a variable allocated
on the stack, the class destructor (if any) is called for that
instance. Neither the garbage collector nor any class deallocator
is called.
)
<h3>Cast Expressions</h3>
$(GRAMMAR
$(GNAME CastExpression):
$(B cast $(LPAREN)) $(LINK2 declaration.html#Type, $(I Type)) $(B $(RPAREN)) $(GLINK UnaryExpression)
$(B cast $(LPAREN)) $(I CastParam) $(B $(RPAREN)) $(GLINK UnaryExpression)
$(GNAME CastParam):
$(LINK2 declaration.html#Type, $(I Type))
$(B const)
$(B const shared)
$(B shared const)
$(B inout)
$(B inout shared)
$(B shared inout)
$(B immutable)
$(B shared)
)
$(P A $(I CastExpression) converts the $(I UnaryExpression)
to $(LINK2 declaration.html#Type, $(I Type)).
)
-------------
$(B cast)(foo) -p; // cast (-p) to type foo
(foo) - p; // subtract p from foo
-------------
$(P Any casting of a class reference to a
derived class reference is done with a runtime check to make sure it
really is a downcast. $(B null) is the result if it isn't.
$(B Note:) This is equivalent to the behavior of the
dynamic_cast operator in C++.
)
-------------
class A { ... }
class B : A { ... }
void test(A a, B b)
{
B bx = a; // error, need cast
B bx = cast(B) a; // bx is null if a is not a B
A ax = b; // no cast needed
A ax = cast(A) b; // no runtime check needed for upcast
}
-------------
$(P In order to determine if an object $(TT o) is an instance of
a class $(TT B) use a cast:
)
-------------
if ($(B cast)(B) o)
{
// o is an instance of B
}
else
{
// o is not an instance of B
}
-------------
$(P Casting a floating point literal from one type to another
changes its type, but internally it is retained at full
precision for the purposes of constant folding.
)
---
void test()
{
real a = 3.40483L;
real b;
b = 3.40483; // literal is not truncated to double precision
assert(a == b);
assert(a == 3.40483);
assert(a == 3.40483L);
assert(a == 3.40483F);
double d = 3.40483; // truncate literal when assigned to variable
assert(d != a); // so it is no longer the same
const double x = 3.40483; // assignment to const is not
assert(x == a); // truncated if the initializer is visible
}
---
$(P Casting a value $(I v) to a struct $(I S), when value is not a struct
of the same type, is equivalent to:
)
---
S(v)
---
$(V2
<h2><a name="PowExpression">Pow Expressions</a></h2>
$(GRAMMAR
$(GNAME PowExpression):
$(GLINK PostfixExpression)
$(GLINK PostfixExpression) ^^ $(GLINK UnaryExpression)
)
$(P $(I PowExpression) raises its left operand to the power of its
right operand.
)
)
<h2>Postfix Expressions</h2>
$(GRAMMAR
$(GNAME PostfixExpression):
$(GLINK PrimaryExpression)
$(I PostfixExpression) $(B .) $(I Identifier)
$(I PostfixExpression) $(B .) $(GLINK2 template, TemplateInstance)
$(I PostfixExpression) $(B .) $(GLINK NewExpression)
$(I PostfixExpression) $(B ++)
$(I PostfixExpression) $(B --)
$(I PostfixExpression) $(B ( ))
$(I PostfixExpression) $(B $(LPAREN)) $(GLINK ArgumentList) $(B $(RPAREN))
$(GLINK IndexExpression)
$(GLINK SliceExpression)
)
<h2>Index Expressions</h2>
$(GRAMMAR
$(GNAME IndexExpression):
$(GLINK PostfixExpression) $(B [) $(GLINK ArgumentList) $(B ])
)
$(P $(I PostfixExpression) is evaluated.
If $(I PostfixExpression) is an expression of type
static array or dynamic array, the symbol $(DOLLAR) is
set to be the the number of elements in the array.
If $(I PostfixExpression) is an $(I ExpressionTuple),
the symbol $(DOLLAR) is
set to be the the number of elements in the tuple.
A new declaration scope is created for the evaluation of the
$(GLINK ArgumentList) and $(DOLLAR) appears in that scope only.
)
$(P If $(I PostfixExpression) is an $(I ExpressionTuple),
then the $(GLINK ArgumentList) must consist of only one argument,
and that must be statically evaluatable to an integral constant.
That integral constant $(I n) then selects the $(I n)th
expression in the $(I ExpressionTuple), which is the result
of the $(I IndexExpression).
It is an error if $(I n) is out of bounds of the $(I ExpressionTuple).
)
<h2>Slice Expressions</h2>
$(GRAMMAR
$(GNAME SliceExpression):
$(GLINK PostfixExpression) $(B [ ])
$(GLINK PostfixExpression) $(B [) $(GLINK AssignExpression) $(B ..) $(GLINK AssignExpression) $(B ])
)
$(P $(I PostfixExpression) is evaluated.
if $(I PostfixExpression) is an expression of type
static array or dynamic array, the variable $(B length)
(and the special variable $(DOLLAR))
is declared and set to be the length of the array.
A new declaration scope is created for the evaluation of the
$(GLINK AssignExpression)..$(GLINK AssignExpression)
and $(B length) (and $(DOLLAR)) appears in that scope only.
)