Difference between revisions of "Cast"

From D Wiki
Jump to: navigation, search
(Added a page just for this one feature of D, as part of my personal campaign to fill the wiki with easy to find information.)
 
Line 1: Line 1:
 
== Casting class types ==
 
== Casting class types ==
  
Casting to a class type ''T'' can from a value can be achieved with the following:
+
Casting to a class type ''T'' from a value can be achieved with the following:
  
 
<syntaxhighlight lang="D">cast(T) value</syntaxhighlight>
 
<syntaxhighlight lang="D">cast(T) value</syntaxhighlight>

Revision as of 21:40, 15 February 2013

Casting class types

Casting to a class type T from a value can be achieved with the following:

cast(T) value

Casting will result in a null reference when the cast cannot be made. (There is no instanceof operator, because it is not needed.)

MyType castedValue = cast(MyType) someValue;

if (castedValue != null)
{
    /* use the casted value */
}