7 ms·
I'm confused by the sudden digression to Smalltalk. I was talking about C. I'll restate my point. Take the GNU C library. You don't own it, you just use it. It
by Tomis02 3y ago
I'm confused by the sudden digression to Smalltalk. I was talking about C. I'll restate my point.
Take the GNU C library. You don't own it, you just use it. It exposes some types (FILE, DIR), and some functions that use these types.
Then, some people look at this API and proclaim that FILE and DIR are (conceptually) objects, and that there's no difference to having 2 C++ classes, it's just syntactic sugar. And that, therefore, C is OOP (or at least the design of libc).
So now I ask - to which of the so-called objects does F(const char, FILE, DIR) belong?
The answer is whatever you want it to be, because they're only objects in your mind, and you can imagine that F belongs to FILE, or to const char, or to all of them, or FILE/DIR stop being objects, and so on. Without syntax for objects (either explicit or implicit by convention), it's up to your imagination to decide what are the objects in your program. But if imagination is the criteria for OO-ness, then any programming language can be declared OO.
OO is such a fuzzy concept that almost every feature and "pillar" is optional except objects. My point is that if a language doesn't have a syntax for objects then it's not actually OO.
You can, of course, apply an OO mindset to your C program, but you can apply an OO mindset to anything Turing-complete, from Haskell to SQL and x86 assembly, so that's not saying much. If you approach everything with an OO mindset, that doesn't mean everything is OO.
Syntax is real, mindset is just in your mind.
- skissane 3y ago> I'm confused by the sudden digression to Smalltalk. I was talking about C. I'll restate my point. You are arguing "C cannot be an OO language because it lets you do X, and an OO language won't let you do X." My counterargument is "Smalltalk lets you do X, but everyone agrees that Smalltalk is an OO language; hence, you claim that if a language lets you do X it can't be an OO language must be false". > So now I ask - to which of the so-called objects does F(const char, FILE, DIR) belong? The answer is whatever you want it to be, because they're only objects in your mind, and you can imagine that F belongs to FILE, or to const char, or to all of them, or FILE/DIR stop being objects, and so on. If a language permits methods/functions which do not belong to any class, does that mean it can't be OO? A class-less method/function is equivalent to a single-method utility class. A language can't be OO if it lets you create a utility class? > Without syntax for objects (either explicit or implicit by convention), it's up to your imagination to decide what are the objects in your program. Consider this C code: // Declarations in counter.h typedef struct { void *privateData; int (*getValue)(Counter*); void (*increment)(Counter*); void (*decrement)(Counter*); void (*destroy)(Counter**); } Counter; Counter* newCounter(); // In main function: Counter* counter = newCounter(); printf("Initial value: %d\n", counter->getValue(counter)); counter->increment(counter); printf("After increment: %d\n", counter->getValue(counter)); counter->decrement(counter); printf("After decrement: %d\n", counter->getValue(counter)); counter->destroy(&counter); assert(counter == NULL); In the above program, why isn't `counter` an object? Assumably "privateData" is just a pointer to an int, but for all you know, the Counter value is actually stored as EBCDIC Roman numerals. > My point is that if a language doesn't have a syntax for objects then it's not actually OO. C in itself has no concept of an "object". But, there are certain conventions you can adopt in C code which make something an "object". My code above is one example of such a convention, there are others – for example, Microsoft COM (an object is a struct whose first member is a pointer to a struct of function pointers, and the remainder of the struct is private to the implementing class).
- gyrovorbis 3y ago^ This, 100%. GTk's GObject type system which powers the entire GNOME stack is a similar object-oriented C type system with a virtual table pointer as the first member of an object, similar to Microsoft COM and C++. The entire Objective-C runtime which is what powers the OO core of the language was also written with a similar purely C type system... It's just a small compiler layer on top...
- Tomis02 3y ago> In the above program, why isn't `counter` an object? > C in itself has no concept of an "object". You answered your own question. Without syntax to say "this is an object", you can take literally ANYTHING Turing-complete, mentally group data+functions and then claim "it looks like an object, therefore it is an object, why wouldn't it be an object". "Excel is OO", "Dwarf fortress is OO", "x86 assembly is OO", "pen and paper is OO". Well, if everything is OO then nothing is OO, you've made it into a meaningless concept. I suggest we stick to reality. The most basic requirement for a language to be OO is to have some explicit or implicit syntax for objects. That seems to reasonably partition languages into OO and non-OO. > You are arguing "C cannot be an OO language because it lets you do X, and an OO language won't let you do X." I'm arguing no such thing, and I never said such a thing. Would appreciate if you didn't put words in my mouth. I'll ask again. In my concrete example, given that FILE and DIR were classified as objects, to which of the so-called objects does F(const char*, FILE, DIR) belong? Possible answers: 1. FILE 2. DIR 3. None of them 4. Both of them 5. They stop being objects 6. New objects are created, and those own F 7.Something else Pick one. I will then pick any other option and argue that, in my opinion, my chosen answer is the correct one. And then it's suddenly a matter of opinion rather than a matter of fact, you won't be able to prove I am wrong, just like I won't be able to prove I'm right. So much for imaginary objects. EDIT > In the above program, why isn't `counter` an object? To drive my point home, why is it an object? Just because you see an object? All I see is a struct containing 5 pointers. Oh, some are function pointers? What's the difference, it's still just a struct containing 5 pointers. You consider it an object, I don't consider it an object, and none of us is wrong. However, an instance of a C++ class containing members and methods is unambiguously an object because the syntax clearly bound them together. Plus, you can do OO things with this object, which further reinforces the idea that it is an object.