Apr. 19th, 2010

rbandrews: (Default)
I should probably wait a few weeks before making this post until I've learned more, but I don't want to.

A thought experiment:

Let's say you're a programmer, in dynamic languages, like Ruby, or Smalltalk. You're sick of dealing with how slow your language is, and how crappy the library bindings are, so you decide, "to hell with this, I'm going back to C!". But, you don't want to give up everything, so you spend a couple weeks hacking together a decent class system in C.

An object in your system is a struct, with a few important pointers: a pointer to its class (which is also an object, like Smalltalk / Ruby), a bunch of function pointers organized by name, a pointer to a struct of members, and so on. You implement late binding by having something like this:

call_method(someObject, "method_name", arg1, arg2);

And that looks up a method in your object's class' method table, or fails over to the superclass, etc. Standard stuff in a Smalltalk-style OO system.

You implement everything with functions like that: set_member(), create_object(), etc. All in C. Nothing hard, nothing really clever. If you're willing to do away with syntax, you can do whatever you want, after all.

So once you have that all working, then you decide you want some syntax after all. You make a really basic preprocessor for your C programs, so you can type:

@class_definition(Whatever)

and it gets translated into:

Class* Whatever = define_class();

Again, all really basic, some extra syntax bolted on to C. You even leave the functions around, because you never know when you'll want to do something you don't have syntax for yet, right? This is fundamentally different from C++. You haven't made a new language, you've just added a couple syntax forms on to something else, that translate directly into function calls. Nothing is unavailable, nothing is magic, nothing is hidden, it's just polished over a little bit.

This, basically, is Objective C. This is how it was made, which explains a lot, like its completely insane syntax for class definitions. Everything in Objective C that deals with objects is just annotations for a preprocessor to translate into C. Which is actually kind of beautiful, when you think about it. It lets them do stuff like late binding and reflection really easily, which are very hard in C++. Impossible, really.

Profile

rbandrews: (Default)
rbandrews

July 2024

S M T W T F S
 123456
78910111213
14151617181920
212223242526 27
28293031   

Style Credit

Page generated Jul. 6th, 2025 05:11 am
Powered by Dreamwidth Studios

Expand Cut Tags

No cut tags