7

VS compiler does not allow to create sealed exposed types for WINMD type library.

Why is this restriction placed ? (I know about sealed types advantages, my question is with respect to Win RT components).

Tilak
  • 30,108
  • 19
  • 83
  • 131

2 Answers2

4

This is an architectural limitation, imposed by COM. Which sits at the core of any WinRT type, they are derived from IUnknown and IInspectable. The problem with COM is that it only supports interface inheritance but not implementation inheritance. Which was a strong COM design goal, implementation inheritance is too fraught with implementation details, including the infamous diamond problem.

There is a way to make inheritance work by delegation, each method in the derived class explicitly calls the corresponding base interface method, but that's very painful to do yourself. But otherwise the way that Windows.UI.Xaml classes implement inheritance.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • That answers the question. special thanks for mentioning diamond problem – Tilak May 08 '12 at 13:05
  • 1
    That's not quite accurate, it is possible to implement unsealed winrt classes, but they cannot be consumed from JavaScript and they require using the low level (IDL) authoring experience, which can be quite challenging from visual studio. In addition all derived classes (sealed or unsealed) MUST ultimately derive from classes in the Windows namespace. – Larry Osterman May 09 '12 at 01:28
  • Can you point to any supporting documenation. I would like to see, how can that be done. In my case, i need it all within VS, but it would be worth to know how things are going in the background – Tilak May 09 '12 at 03:15
1

I believe the reason is because exposed types should be usable from all different kind of languages(C#, C++, JavaScript any maybe more in the future).

So if you have a class, then one of the use of a class is overriding it into new class. I could want to overide class, that is done in different language. But this is a problem. How do you want to override a base class done in C#, by inheritor class done in C++? This can never work, because both of those have completly different and incompatible OOP implementations.

By forcing exposed classes to be sealed, you remove this problem and make sure people won't try to do something like this.

I'm sure there are much more fundamental things than this, but this is what came to my mind first.

Euphoric
  • 12,645
  • 1
  • 30
  • 44