3 ms·
Objects as encapsulation are fine. The author's claim that modules do the same thing is incorrect. Modules don't, in most languages, have instances. You get on
by Animats 6y ago
Objects as encapsulation are fine. The author's claim that modules do the same thing is incorrect. Modules don't, in most languages, have instances. You get one set of data. Bundling data with functions is way better than global data.
Inheritance got caught up in the "is-a" mindset of 1980s AI, and turned out to be only marginally useful. Being able to have an object as a field of another object is almost as useful. A point I make occasionally is that there needs to be a good way to find the owning object when you do that. Is there a language where, when B is a field of A, you can find A from B? I've plugged for syntax for finding the owner in Rust, where backpointers are difficult.
Order of initialization for inherited objects is a huge headache. If B inherits from A, what in A is B allowed to call or reference during construction? This sort of thing needs language support. Same for order of destruction.
Multiple inheritance is usually a mistake. If you reach the "diamond pattern", your code is too cute.
- formerly_proven 6y ago> A point I make occasionally is that there needs to be a good way to find the owning object when you do that. Is there a language where, when B is a field of A, you can find A from B? I've built this multiple times in Python in a few different ways. Metaclasses work (for e.g. declarative stuff, like in ORMs and data description), the descriptor protocol works for the runtime kind (caveats ensue). > Multiple inheritance is usually a mistake. Allowing multiple inheritance seems to work better in some languages than others. For example, in C++ it almost always seems to be a bad idea. In Python it tends to work out more often (usually when the other classes are pure mix-ins).
- fanf2 6y agoAbstract data types in the sense of CLU / Liskov give you modularity and encapsulation without being object-oriented.
- garethrowlands 6y agoTrue dat. It's common to conflate abstract data types and objects. Similarly, it's common to conflate types and classes.
- ogre_codes 6y ago> You get one set of data. Bundling data with functions is way better than global data. I'm not sure what language you are talking about where you have "one set of data", maybe BASIC? There are almost no modern languages save perhaps Javascript that rely on global data extensively. (and even modern Javascript has moved away from use of global variables).
- Jtsummers 6y ago> Modules don't, in most languages, have instances. You get one set of data. Bundling data with functions is way better than global data. Modules usually have data types that can be instantiated via functions/procedures in the module that let you avoid global data. They don't have to have data fields in them that are changed directly by the procedures (giving you, effectively, singletons).
- Traubenfuchs 6y ago> is there a language where The Hibernate ORM framework for Java supports this paradigm.
- marwis 6y ago> Is there a language where, when B is a field of A, you can find A from B? It's not unusual in system/kernel C/C++ code to do that with offsetof() macro.
- pjmlp 6y ago> Multiple inheritance is usually a mistake. Most mainstream OOP languages support it in some form, and it is quite useful, specially when considering single responsibility principles. Like everything else, it can get abused.