Dynamic binding -- Qt's Signals and Slots vs Objective-C
Bill Baxter
wbaxter at gmail.com
Thu Sep 21 10:10:56 PDT 2006
There seems to be a lot in common between Qt's signals and slots (Qt
S&S) and Objective-C's messaging system. Both basically offer a way to
call a method using a string rather than a pointer, i.e they allow
run-time binding and dispatch. Both seem to be considered Really Good
Things by those who use them, primarily for the purpose of creating GUIs.
So is there anyone out there knowledgeable about both who could compare
and contrast? This link from Fredrik, http://tinyurl.com/kojmm, helped
me understand Obj-C a little, but I've got more questions:
- Is Qt S&S a subset of Obj-C's messaging?
- Could Qt S&S be implemented using Obj-C messaging?
- Could Obj-C-like messaging be implemented using Qt S&S?
- Qt S&S includes an observer pattern in that emit() will call all
connected slots. Is this built into Obj-C messaging too?
- Qt requires derivation from QObject to participate in S&S. Does Obj-C
also require deriving from a particular base object? Is it explicit?
- What is the overhead to support Obj-C messaging in a class? Is there
a way to create an Obj-C class that doesn't support messaging?
- Do people use Obj-C messaging for anything besides GUIs?
The goal of this questioning, of course, is to encourage discussion
about whether these or any other form dynamic binding could / should be
added to D.
I've heard many people in C++ land say things to the effect of "Bah,
those silly Qt people weren't smart enough realize that you can
implement signals and slots directly in C++". But I've not been
particularly impressed by the pure C++ S&S libraries I've seen. On the
other hand, I found Qt's S&S to be simple, clean, straightforward, and
very easy to use. And I've not heard anything but praise about making
GUIs with Obj-C. Lutger's S&S for D posted earlier looks very nice, but
there must be something neat that Qt S&S--being dynamic--can do that
isn't possible with the static S&S solutions. Like enumerating
available signals for use in a GUI builder tool.
- Is it possible to mix D-style classes with Obj-C style messaging?
Apple's Objective-C++ apparently doesn't allow it.
http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html
I.e. a class is either a C++ class or an Obj-C class. They can call
each other, but they are separate. You can't send an Obj-C message to a
C++ class. I think the ideal, though, would be something closer to Qt
where you have the ability to make any method you want be a slot. In Qt
those methods remain valid C++ methods, but you can also call them by
name, effectively.
I think what it comes down to is that you want to have as many tools
available as possible in your toolbox. Some things are just easier to
do dynamically (for flexibility and loose coupling), and other things
are better done statically (for runtime speed and robust compile-time
type-checking). I think that explains the growing popularity of hybrids
like Jython and JRuby. C# probably fits in there somewhere too.
--bb
More information about the Digitalmars-d
mailing list