| > Making Scheme object-oriented is a sophomore homework assignment. On the other hand, adding object orientation to C requires the programming chops of Bjarne Stroustrup. C++ added a ton of additional stuff to C. The original Objective-C is a much simpler approach on adding object orientation to C and at its core it'd all be about finding expressions like [foo bar:baz boo:hoo]
and replacing them with something like obj_send(foo, msg_foobarboo, baz, hoo);
and linking with an underlying runtime that handles message passing of course.An alternative would be Turbo Pascal 5.5-style OOP where struct foo : base {
foo();
~foo();
void blah();
}
foo::foo(){ base(); }
foo::~foo(){ ~base(); }
void foo::blah(){}
would be accepted but aside from the C++-like syntax for constructors, destructors (which handle the hidden VMT field) and methods nothing is done automatically and instead you are expected to explicitly construct and destroy the objects (if needed), e.g. struct foo foo;
foo.foo(); /* call constructor chain */
foo.~foo(); /* call destructor chain */
or struct foo* foo = malloc(sizeof(struct foo));
foo->foo(); /* call constructor chain */
foo->~foo(); /* call destructor chain */
free(foo);
Note that Objective-C handles (or handled at the past, not sure how things changed nowadays) allocation and construction in separate steps too.Of course unlike a language with Lisp-like macros you'd need to modify a compiler or write a preprocessor to do the above, which makes it a bit harder, but in any case adding object orientation to C does not mean you have to make it as complex as C++. |
Dynamic typing, arbitrary messages, and no memory leaks. Just like Lisp. It's not perfect (string message could have typos, and might use %p for objects), but it's not too bad.
[0] https://en.wikipedia.org/wiki/Boehm_garbage_collector