Re: Safe cast

2020-03-06 Thread Adam D. Ruppe via Digitalmars-d-learn
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

Re: Safe cast

2020-03-06 Thread drug via Digitalmars-d-learn
It's too complex On 3/6/20 3:45 PM, Виталий Фадеев wrote: On Friday, 6 March 2020 at 12:35:29 UTC, Виталий Фадеев wrote: Searching info for object casting with checking class type at runtime. Like this: class A {     // } class B {     int bVar; } unittest {     A a = new A();     A x =

Re: Safe cast

2020-03-06 Thread Виталий Фадеев via Digitalmars-d-learn
On Friday, 6 March 2020 at 12:35:29 UTC, Виталий Фадеев wrote: Searching info for object casting with checking class type at runtime. Like this: class A { // } class B { int bVar; } unittest { A a = new A(); A x = cast( A )a; // ok A x = cast( B )a; // ok, but

Safe cast

2020-03-06 Thread Виталий Фадеев via Digitalmars-d-learn
Searching info for object casting with checking class type at runtime. Like this: class A { // } class B { int bVar; } unittest { A a = new A(); A x = cast( A )a; // ok A x = cast( B )a; // ok, but unsafe A x = safeCast( B )a; // throw exception A x =