March 18, 2020
On 18/03/2020 12:59 AM, Steven Schveighoffer wrote:
> I think he's looking for object pattern matching. i.e. you give it an Object, and it runs a block of code based on the derived type.

In case this syntax is unknown:

if (MyObject1 myObject = cast(MyObject1)obj) {
	...
} else if (MyObject2 myObject = cast(MyObject2)obj) {
	...
} else {
	...
}
March 17, 2020
On 3/17/20 8:04 AM, rikki cattermole wrote:
> On 18/03/2020 12:59 AM, Steven Schveighoffer wrote:
>> I think he's looking for object pattern matching. i.e. you give it an Object, and it runs a block of code based on the derived type.
> 
> In case this syntax is unknown:
> 
> if (MyObject1 myObject = cast(MyObject1)obj) {
>      ...
> } else if (MyObject2 myObject = cast(MyObject2)obj) {
>      ...
> } else {
>      ...
> }

Yes, look a few posts up, that was my recommendation.

I don't think there's a "pleasant" way to do this in D. You can do it with delegates, but that's a bit uglier than a switch statement, and has been rejected by the OP. However, I don't think the syntax requested is going to be added to D, as it's an odd syntax:

case SomeType inst: // inst now defined as SomeType

So that's probably the best bet.

-Steve
1 2
Next ›   Last »