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 and true are automatically converted to 0 and 1, respectively. Similarly, zero values are automatically converted to false and nonzero values are automatically converted to true.

  • null references are automatically converted to false and non-null references are automatically converted to true.

  • 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 to immutable without copying.

  • The cast operator is the most powerful conversion tool.

Get hands-on with 1200+ tech skills courses.