Difference between revisions of "Cast"
Line 9: | Line 9: | ||
<syntaxhighlight lang="D">MyType castedValue = cast(MyType) someValue; | <syntaxhighlight lang="D">MyType castedValue = cast(MyType) someValue; | ||
− | if (castedValue ! | + | 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 */
}