root/trunk/docsrc/type.dd

Revision 2137, 8.4 kB (checked in by walter, 2 years ago)

bugzilla 632 Typedef/enum promotions spec ambiguous - ultimate base type or lowest common denominator?

  • Property svn:eol-style set to native
Line 
1 Ddoc
2
3 $(SPEC_S Types,
4
5 $(SECTION2 Basic Data Types,
6
7     $(TABLE2 Basic Data Types,
8     $(TR $(TH Keyword) $(TH Description) $(TH Default Initializer (.init))
9     )
10     $(TR
11         $(TD $(TT void))
12         $(TD no type)
13         $(TD -)
14     )
15     $(TR
16         $(TD $(TT bool))
17         $(TD boolean value)
18         $(TD false)
19     )
20     $(TR
21         $(TD $(TT byte))
22         $(TD signed 8 bits)
23         $(TD 0)
24     )
25     $(TR
26         $(TD $(TT ubyte))
27         $(TD unsigned 8 bits)
28         $(TD 0)
29     )
30     $(TR
31         $(TD $(TT short))
32         $(TD signed 16 bits)
33         $(TD 0)
34     )
35     $(TR
36         $(TD $(TT ushort))
37         $(TD unsigned 16 bits)
38         $(TD 0)
39     )
40     $(TR
41         $(TD $(TT int))
42         $(TD signed 32 bits)
43         $(TD 0)
44     )
45     $(TR
46         $(TD $(TT uint))
47         $(TD unsigned 32 bits)
48         $(TD 0)
49     )
50     $(TR
51         $(TD $(TT long))
52         $(TD signed 64 bits)
53         $(TD 0L)
54     )
55     $(TR
56         $(TD $(TT ulong))
57         $(TD unsigned 64 bits)
58         $(TD 0L)
59     )
60     $(TR
61         $(TD $(TT cent))
62         $(TD signed 128 bits (reserved for future use))
63         $(TD 0)
64     )
65     $(TR
66         $(TD $(TT ucent))
67         $(TD unsigned 128 bits (reserved for future use))
68         $(TD 0)
69     )
70     $(TR
71         $(TD $(TT float))
72         $(TD 32 bit floating point)
73         $(TD float.nan)
74     )
75     $(TR
76         $(TD $(TT double))
77         $(TD 64 bit floating point)
78         $(TD double.nan)
79     )
80     $(TR
81         $(TD $(TT real))
82         $(TD largest hardware implemented floating
83         point size ($(B Implementation Note:) 80 bits for x86 CPUs)
84         or double size, whichever is larger)
85         $(TD real.nan)
86     )
87     $(TR
88         $(TD $(TT ifloat))
89         $(TD imaginary float)
90         $(TD float.nan * 1.0i)
91     )
92     $(TR
93         $(TD $(TT idouble))
94         $(TD imaginary double)
95         $(TD double.nan * 1.0i)
96     )
97     $(TR
98         $(TD $(TT ireal))
99         $(TD imaginary real)
100         $(TD real.nan * 1.0i)
101     )
102     $(TR
103         $(TD $(TT cfloat))
104         $(TD a complex number of two float values)
105         $(TD float.nan + float.nan * 1.0i)
106     )
107     $(TR
108         $(TD $(TT cdouble))
109         $(TD complex double)
110         $(TD double.nan + double.nan * 1.0i)
111     )
112     $(TR
113         $(TD $(TT creal))
114         $(TD complex real)
115         $(TD real.nan + real.nan * 1.0i)
116     )
117     $(TR
118         $(TD $(TT char))
119         $(TD unsigned 8 bit UTF-8)
120         $(TD 0xFF)
121     )
122     $(TR
123         $(TD $(TT wchar))
124         $(TD unsigned 16 bit UTF-16)
125         $(TD 0xFFFF)
126     )
127     $(TR
128         $(TD $(TT dchar))
129         $(TD unsigned 32 bit UTF-32)
130         $(TD 0x0000FFFF)
131     )
132     )
133 )
134
135
136 $(SECTION2 Derived Data Types,
137
138     $(UL
139     $(LI pointer)
140     $(LI array)
141     $(LI associative array)
142     $(LI function)
143     $(LI delegate)
144     )
145
146     $(P $(LINK2 arrays.html#strings, $(I Strings)) are a special case of arrays.)
147 )
148
149 $(SECTION2 User Defined Types,
150
151     $(UL
152     $(LI alias)
153     $(LI enum)
154     $(LI struct)
155     $(LI union)
156     $(LI class)
157     $(V1 $(LI typedef))
158     )
159 )
160
161
162 $(SECTION2 Base Types,
163
164     $(P The $(I base type) of an enum is the type it is based on:)
165
166 ---
167 enum E : T { ... }  // T is the $(I base type) of E
168 ---
169
170 $(V1
171     $(P The $(I base type) of a typedef is the type it is formed from:)
172
173 ---
174 typedef T U;        // T is the $(I base type) of U
175 ---
176 )
177 )
178
179
180 $(SECTION2 Pointer Conversions,
181
182     $(P Casting pointers to non-pointers and vice versa is allowed in D,
183     however, do not do this for any pointers that point to data
184     allocated by the garbage collector.
185     )
186 )
187
188 $(SECTION2 Implicit Conversions,
189
190     $(P Implicit conversions are used to automatically convert
191     types as required.
192     )
193
194     $(P A $(V1 typedef or) enum can be implicitly converted to its base
195     type, but going the other way requires an explicit
196     conversion.
197     A literal can be implicitly converted to a typedef.
198     For example:
199     )
200
201 -------------------
202 $(V1 typedef int myint;
203 int i;
204 myint m;
205 i = m;          // OK
206 m = i;          // error
207 m = cast(myint)i;   // OK
208 m = 3;                  // OK
209 )
210 enum Foo { E }
211 Foo f;
212 i = f;          // OK
213 f = i;          // error
214 f = cast(Foo)i;     // OK
215 f = 0;                  // error
216 f = E;                  // OK
217 -------------------
218 )
219
220
221 $(SECTION2 Integer Promotions,
222
223     $(P Integer Promotions are conversions of the following types:
224     )
225
226     $(TABLE2 Integer Promotions,
227     $(TR
228     $(TH from)
229     $(TH to)
230     )
231     $(TR
232     $(TD bool)
233     $(TD int)
234     )
235     $(TR
236     $(TD byte)
237     $(TD int)
238     )
239     $(TR
240     $(TD ubyte)
241     $(TD int)
242     )
243     $(TR
244     $(TD short)
245     $(TD int)
246     )
247     $(TR
248     $(TD ushort)
249     $(TD int)
250     )
251     $(TR
252     $(TD char)
253     $(TD int)
254     )
255     $(TR
256     $(TD wchar)
257     $(TD int)
258     )
259     $(TR
260     $(TD dchar)
261     $(TD uint)
262     )
263     )
264
265     $(P If a $(V1 typedef or) enum has as a base type one of the types
266     in the left column, it is converted to the type in the right
267     column.
268     )
269 )
270
271
272 $(SECTION2 Usual Arithmetic Conversions,
273
274     $(P The usual arithmetic conversions convert operands of binary
275     operators to a common type. The operands must already be
276     of arithmetic types.
277     The following rules are applied
278     in order, looking at the base type:
279     )
280
281     $(OL
282     $(LI If either operand is real, the other operand is
283     converted to real.)
284
285     $(LI Else if either operand is double, the other operand is
286     converted to double.)
287
288     $(LI Else if either operand is float, the other operand is
289     converted to float.)
290
291     $(LI Else the integer promotions are done on each operand,
292     followed by:
293
294     $(OL
295         $(LI If both are the same type, no more conversions are done.)
296
297         $(LI If both are signed or both are unsigned, the
298         smaller type is converted to the larger.)
299
300         $(LI If the signed type is larger than the unsigned
301         type, the unsigned type is converted to the signed type.)
302
303         $(LI The signed type is converted to the unsigned type.)
304     )
305     )
306     )
307
308     $(P If one or both of the operand types is an enum $(V1 or typedef) after
309     undergoing the above conversions, the result type is:)
310
311     $(OL
312     $(LI If the operands are the same type, the result will be the
313     that type.)
314     $(LI If one operand is an enum $(V1 or typedef) and the other is the base type
315     of that $(V1 typedef or) enum, the result is the base type.)
316     $(LI If the two operands are different $(V1 typedefs or) enums,
317     the result is the closest base type common to both. A base type being closer
318     means there is a shorter sequence of conversions to base type to get there from the
319     original type.)
320     )
321
322     $(P Integer values cannot be implicitly converted to another
323     type that cannot represent the integer bit pattern after integral
324     promotion. For example:)
325
326 ---
327 ubyte  u1 = cast(byte)-1;   // error, -1 cannot be represented in a ubyte
328 ushort u2 = cast(short)-1;  // error, -1 cannot be represented in a ushort
329 uint   u3 = cast(int)-1;    // ok, -1 can be represented in a uint
330 ulong  u4 = cast(ulong)-1;  // ok, -1 can be represented in a ulong
331 ---
332
333     $(P Floating point types cannot be implicitly converted to
334     integral types.
335     )
336
337     $(P Complex floating point types cannot be implicitly converted
338     to non-complex floating point types.
339     )
340
341     $(P Imaginary floating point types cannot be implicitly converted to
342     float, double, or real types. Float, double, or real types
343     cannot be implicitly converted to imaginary floating
344     point types.
345     )
346 )
347
348
349 $(SECTION2 bool,
350
351     $(P The bool type is a 1 byte size type that can only hold the
352     value $(D_KEYWORD true) or $(D_KEYWORD false).
353     The only operators that can accept operands of type bool are:
354     & | ^ &= |= ^= ! && || ?:.
355     A bool value can be implicitly converted to any integral type,
356     with $(D_KEYWORD false) becoming 0 and $(D_KEYWORD true) becoming 1.
357     The numeric literals 0 and 1 can be implicitly
358     converted to the bool values $(D_KEYWORD false) and $(D_KEYWORD true),
359     respectively.
360     Casting an expression to bool means testing for 0 or !=0 for
361     arithmetic types, and $(D_KEYWORD null) or !=$(D_KEYWORD null)
362     for pointers or references.
363     )
364 )
365
366
367 $(SECTION2 <a name="delegates">Delegates</a>,
368
369     $(P There are no pointers-to-members in D, but a more useful
370     concept called $(I delegates) are supported.
371     Delegates are an aggregate of two pieces of data: an
372     object reference and a function pointer. The object reference
373     forms the $(I this) pointer when the function is called.
374     )
375
376     $(P Delegates are declared similarly to function pointers,
377     except that the keyword $(B delegate) takes the place
378     of (*), and the identifier occurs afterwards:
379     )
380
381 -------------------
382 int function(int) fp;   // fp is pointer to a function
383 int delegate(int) dg;   // dg is a delegate to a function
384 -------------------
385
386     $(P The C style syntax for declaring pointers to functions is
387     also supported:
388     )
389
390 -------------------
391 int (*fp)(int);     // fp is pointer to a function
392 -------------------
393
394     $(P A delegate is initialized analogously to function pointers:
395     )
396
397 -------------------
398 int func(int);
399 fp = &func;     // fp points to func
400
401 class OB
402 {   int member(int);
403 }
404 OB o;
405 dg = &o.member;     // dg is a delegate to object $(I o) and
406             // member function $(I member)
407 -------------------
408
409     $(P Delegates cannot be initialized with static member functions
410     or non-member functions.
411     )
412
413     $(P Delegates are called analogously to function pointers:
414     )
415
416 -------------------
417 fp(3);      // call func(3)
418 dg(3);      // call o.member(3)
419 -------------------
420 )
421
422 )
423
424 Macros:
425     TITLE=Types
426     WIKI=Type
Note: See TracBrowser for help on using the browser.