Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

regarding ad hoc unions the article mentions this:

  Siamese pet = ...;
  (Cat or Chihuahua) mostlyCats = pet;
  Dog dog = (Dog)mostlyCats;
Note: This works for implemented interfaces too.

I am unsure what that section entails...

Let's say I have a function like this:

  void ConsumeAB<T>(T ab) where T: IA, IB;  
And implementing classes like this:

  class ABC : IA, IB {}
  class ABD : IA, IB {}
Will I be able to use this function like this:

        var items = new (ABC or ABD)[] { new ABC(), new ABD() };
        foreach (var item in items)
        {
            ConsumeAB(item);
        }
In other words will the ad hoc union act as it would implement all interfaces that all cases have in common? My guess it won't work, as it gets lowered to code that uses object. Would a custom union help me here?


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: