2

I've watched some videos and articles on metaclasses , and IMO they have the potential to replace inheritance and providing a way to do a sort of compile-time inheritance.

With metaclasses i can provide interfaces, default function implementation, and even correctness of implementation(not the body of the functions of course) of class during compilation. So there is something that can't be done in metaclasses that can be done with inheritance, polymorphism and OO stuff?

rebellion
  • 39
  • 4
  • 2
    IIUIC, proper polymorphism cannot be done with metaclasses: you cannot have a `std::list` and store (in the same list) objects of different types that implement that metaclass. – rodrigo Aug 18 '17 at 11:10
  • can you give an example? Especially "and even correctness of implementation [...] of class during compilation" is not clear how a metaclass can do something that you cannot do without – 463035818_is_not_an_ai Aug 18 '17 at 11:10
  • for example forcing method to be all public – rebellion Aug 18 '17 at 11:19
  • @rodrigo: you probably mean runtime or dynamic polymorphism (https://stackoverflow.com/questions/2128838/compile-time-polymorphism-and-runtime-polymorphism, https://stackoverflow.com/questions/16875989/compile-time-vs-run-time-polymorphism-in-c-advantages-disadvantages) which according to me certainly needs inheritance. – stefaanv Aug 18 '17 at 11:21
  • 2
    @stefaanv: Yes, of course. When I said _proper_ I meant _runtime_ or _dynamic_ polymorphism. I'm not implying that `_static_` polymorphism is not a proper thing... – rodrigo Aug 18 '17 at 11:24
  • 1
    Python has both concepts (inheritance and metaclasses) and they serve different use cases. I do not know why one should replace the other. You can certainly plant a nail with a shovel, but a hammer would be more adequate... – Serge Ballesta Aug 18 '17 at 11:48
  • type-erasure needs dynamic polymorphism. Things like unifying a bunch of lambdas onto the `std::function` interface – Caleth Aug 18 '17 at 12:44
  • i've forgot about container classes and type erasure – rebellion Aug 18 '17 at 15:37

1 Answers1

1

Just as a general remark, I write as a "frequent" author of Python metaclasses: Metaclasses are not meant for "day to day" use, and however C++ metaclasses may not be the same as they are in Python, I can hardly see such a concept be used to replace something as common as inheritance.

Inheritance have their roles. If you are in need for special rules for a whole class hierarchy, it may be useful to have a metaclass to specify these rules to start with. (The first example in both the C++ proposal and on most explanatory material I've browsed are "interfaces" - which mandates that all methods are virtual). So, supposing you find out a special rule you want in a whole set of classes in your system and can express that using a metaclass, that does not preclude you from creating a single base-class from that metaclass, and create all other classes with "normal" inheritance, if what will change from one such class to another are just the usual method overriding and specialization: inheritance still will be simpler, both to code, as to whoever reads your code, as for the toolchain echo system that evolved around C++ in decades of language existance.

So, yes, metaclass inheritance could, for all that is, allow you to encode all common capabilities of your classes in the metaclass, and them just create new classes using this metaclass, instead of inheritance. But there are no motives on Earth to do that.

Just to bridge back to Python, where I am well acquainted with the concept: the language recently took one step in the opposite direction - by enabling two mechanisms in normal inheritance in version 3.6 that were previously only possible through metaclasses, and in doing that, further reducing the use cases where metaclasses are needed just due to the extra complication they necessarily call for.

jsbueno
  • 99,910
  • 10
  • 151
  • 209