Difference between revisions of "Cast"

From D Wiki
Jump to: navigation, search
Line 9: Line 9:
 
<syntaxhighlight lang="D">MyType castedValue = cast(MyType) someValue;
 
<syntaxhighlight lang="D">MyType castedValue = cast(MyType) someValue;
  
if (castedValue != null)
+
if (castedValue !is null)
 
{
 
{
 
     /* use the casted value */
 
     /* use the casted value */
 
}</syntaxhighlight>
 
}</syntaxhighlight>

Revision as of 21:43, 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 !is null)
{
    /* use the casted value */
}