This post originated from an RSS feed registered with Agile Buzz
by Keith Ray.
Original Post: Objective-C++
Feed Title: MemoRanda
Feed URL: http://homepage.mac.com/1/homepage404ErrorPage.html
Feed Description: Keith Ray's notes to be remembered on agile software development, project management, oo programming, and other topics.
I used to be a master of C++, even serving as a team's C++ "language lawyer"... As the language has become more esoteric, I have purposefully avoided getting into the rat-hole of meta-programming in favor of "real" programming.
Now that I'm back on the Mac doing Cocoa programming, I'm getting more work done with less keyboarding because I can use Objective-C in combination with C++. Objective-C has more run-time power and flexibility than C++.
Patrick Logan reminds us how easy it is to integrate Objective-C's first-class objects with Python, Ruby, Java, Smalltalk/Fscript... integration that does not require, for the most part,
the massive amounts of code-generation that integrating C++ objects to other systems would require.
Smalltalk and Ruby's "blocks" (closures) provide a lot of power in a much more simple manner than C++ metaprogramming provides... besides passing a block around to classes and methods, you can implement new methods on blocks, to provide new control structures. This provides some sample of blocks.
Smalltalk, Ruby, Python, and Objective-C have ways for a class to intercept "invalid" method calls and do useful things with them - such as forwarding the call over a network (a network-proxy via a single method). To do network proxies in C++ requires code-generation or extensive amounts of template programming. See these Smalltalk examples for a lot of other interesting things that can be done with this interception capability that would require re-writing the compiler in another language.
I have been doing some meta programming in Objective-C this weekend. Unlike in C++, meta-programming is done in the same syntax as regular programming. Starting with the Objective-c proxy example here, I'm creating a class to log calls into an object's methods. This involves decoding the method signatures in order to log the arguments. I will be able to do something like this:
to take an existing object and start logging calls to it -- no recompilation of the sources of someObj's class would be needed. I might even be able to call this code from gdb, so my choice to start logging an object could be made at run-time when debugging.
Some parts of Aspect Oriented Programming are doable in Objective-C without using a pre-parser/code-generator, by using the forwarding/proxy and introspection features of the language.
Apple's Objective-C++ lets me use straight C, Objective-C objects/syntax, and C++ objects/syntax, and mix them in the same source files. (With some care due to differences in object-allocation and exception-handling.) Objective-C++ lets you Alternative Hard(fast) and Soft(slow) Layers within the same language.
Apple made their modifications of gcc available to the maintainers of gcc (and the sources are probably on http://developer.apple.com/darwin/ or opendarwin.org), but the last time I checked, the maintainers of gcc declined to accept those changes and make those modifications available on other platforms. If you want the power of Objective-C++, but you're not using MacOS, ask the gcc maintainers to accept Apple's mods -- or buy a Mac. :-)