Safe cast

Adam D. Ruppe destructionator at gmail.com
Fri Mar 6 13:31:23 UTC 2020


On Friday, 6 March 2020 at 13:03:22 UTC, drug wrote:
> Here x will be null. You can use `enforce(x !is null);` if you 
> want exception.

or since enforce returns it thing, just do

B b = enforce(cast(B) x);

you can also check easily in if statements:

if(auto b = cast(B) x) {
    // x was a b, use b in here
} else {
   //  x was not b, try something else
}


More information about the Digitalmars-d-learn mailing list