On 3/15/20 2:22 PM, 12345swordy wrote:
On Sunday, 15 March 2020 at 17:55:59 UTC, Steven Schveighoffer wrote:
On 3/14/20 3:04 PM, 12345swordy wrote:
I.E.

switch (object)
     case Type1 t1:
     case Type2 t2:
     case Type3 t3:


Is this a class object and you are trying to determine at runtime which derived type it is and perform an action based on that? Or are you trying to switch on the type of a concrete item?

It should technically be possible to use the fully qualified name to switch on, but I don't think typeid(Type1).name is usable as a switch label.


https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching

It is an example from c#.
object is the top type in that language.
https://en.wikipedia.org/wiki/Top_type

D doesn't support this natively. The closest you can get is something akin to what aliak wrote (you would need to write something, not sure if Phobos or some package has implemented the feature), or use cascaded if statements:

if(auto t1 = cast(Type1)object)
        // use t1 as Type1
else if(auto t2 = cast(Type2)object)
        // use t2 as Type2
...

Taking care of course to look for most derived types first.

a switch based on static or runtime type would be really cool for D to support.

-Steve

Reply via email to