Summary
This lesson summarizes our discussion on type conversions.
-
Automatic type conversions are mostly in the safe direction: From the narrower type to the wider type and from mutable to
const
. -
Conversions to unsigned types may have surprising effects because unsigned types cannot have negative values.
-
enum
types can automatically be converted to integer values, but the opposite conversion is not automatic. -
false
andtrue
are automatically converted to0
and1
, respectively. Similarly, zero values are automatically converted tofalse
and nonzero values are automatically converted totrue
. -
null
references are automatically converted tofalse
and non-null references are automatically converted totrue
. -
The construction syntax can be used for explicit conversions. For example:
void * v; // ... int * p = cast(int*)v;
-
to()
covers most of the explicit conversions. -
assumeUnique()
converts toimmutable
without copying. -
The
cast
operator is the most powerful conversion tool.
Get hands-on with 1400+ tech skills courses.