Re: Dynamic Languages [was: Re: why Obj-C]
Re: Dynamic Languages [was: Re: why Obj-C]
- Subject: Re: Dynamic Languages [was: Re: why Obj-C]
- From: Thomas Lachand-Robert <email@hidden>
- Date: Sat, 6 Apr 2002 15:18:17 +0200
Le samedi 6 avril 2002, ` 10:29 , Matthew Johnson a icrit :
A shared object is a Dynamic library.
class Integer
{
int i
setValue(int n)
addValue(Integer j)
}
Typedef struct
{
int i;
void (*setValueFuncPtr)(int n);
void (*addValue)(int j);
} Integer;
I can create a object from both these guys.
The one thing I have to do extra for the struct is associate methods with
the two function pointers. Kinda cool dynamic class really.
So you see, you actually know what dynamic objects are! Your
implementation is actually some sort of rebuild of Obj-C, except that
(sorry to say that), Obj-C does that better. In particular, your "struct
Integer" suffers from fatness: every object uses 12 bytes of RAM, and more
if you add other functions ;-). Anyway, this is a very good example of a
programmer who really needs the dynamism offered easily by Obj-C, but is
struggling with C++ which cannot offer that. I have also seen a number of
programs where the retain/release mechanism is redefined (painfully) in C+
+, though it is given for free in Obj-C.
You got a number of theoretical answers to your original question "why
Obj-C", I would like to complement with practical exercises: try to do
them in any language you like. I can tell you the solution in Obj-C is, in
each case, only one or two lines of code.
Exercise 1. You are using a standard OOP library, say with a "string"
class. However, you need in some different places of your code a method to
strip any combination of spaces/tabs/newlines at the end, but the
corresponding class does not have this method. What do you do?
HINT: in Objective-C, you can add a new method to ANY existing class.
Exercise 2: In the same library, there is a method for removing spaces at
the beginning, but unfortunately this method has a bug. What do you do?
HINT: in Objective-C, you can override any method of any existing class.
Exercise 3: You designed a class whose object have different contents or
different sizes, depending on which "constructor" is called (or on the
value of the constructors). What do you do?
HINT: In Objective-C, the "init" method is just a method. It can assign to
self and return a different object if needed.
Exercise 4: You read (in standard input, in a file, in a GUI, whatever) a
number of class names, each with an associated method name. All of these
are ordinary strings. You need to allocate a new instance of every given
class, and call the associated method for that instance (possibly after
verifying the method does actually exist). What do you do?
HINT: in Objective-C, you can get a Class object (and the instances) from
the name, and you can call a method from its name also. You don't need any
knowledge about the class or the methods for that. You can also check if
the class or the instances actually implement this method.
Maybe you think the latter example is silly, but that's exactly what
Interface Builder does for you, as a user of IB; all the "magic" of IB is
there. If you think for a while about this later example, you will see
that they are plenty of very insteresting applications of that, most of
them in complicated problems. The fact that Objective-C allows you to do
that so easily opens a bunch of great perspectives.
P.S.: I still don't like the brackets, too (plus, I'm french, and there
are difficult to get on my keyboard). But the more I use Obj-C, the more I
like it. It is much better than Java IMHO, even as a pure language.
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.