www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - safe matching on subclass type

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Why isn't polymorphic down-casts such as in the following pattern 
matching on sub-class

class Base {}
class Sub : Base {}

 safe unittest
{
     auto base = new Base();
     if (auto sub = cast(Sub)base)
     {
         // use sub
     }
}

allowed in safe D?
Jan 17 2018
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 17 January 2018 at 13:19:42 UTC, Nordlöw wrote:
 Why isn't polymorphic down-casts such as in the following 
 pattern matching on sub-class

 class Base {}
 class Sub : Base {}

  safe unittest
 {
     auto base = new Base();
     if (auto sub = cast(Sub)base)
     {
         // use sub
     }
 }

 allowed in safe D?
Just discovered that the base instance was const. My mistake...
Jan 17 2018