Article View: comp.lang.objective-c
Article #355comp.lang.objective-c FAQ, part 1/3: Answers
From: Tiggr
Date: Mon, 20 Dec 1993 16:26
Date: Mon, 20 Dec 1993 16:26
630 lines
26731 bytes
26731 bytes
Archive-name: Objective-C/answers
Version: $Id: answers,v 2.7 1993/12/01 07:34:52 tiggr Exp $
Answers to
FREQUENTLY ASKED QUESTIONS
concerning Objective-C
This is the first in a series of three informational postings concerning
comp.lang.objective-c. This first part answers FAQs; the second part lists
available class libraries and the third part is a simple sample Objective-C
program.
This posting answers the following questions:
1 What is Objective-C?
2 What is the difference between Objective-C and C?
3 What is the difference between Objective-C and C++?
4 What exactly is it that makes Objective-C have `classes similar to
Smalltalk', and what are the resulting capabilities of Objective-C?
5 What are the `nice features' of Objective-C?
6 What are some of the common problems of the language and how can I work
around them?
7 What are Protocols?
8 What is the difference between the NeXTSTEP, Stepstone and GNU CC
versions of Objective-C?
9 What written information concerning Objective-C is available?
10 What kind of Objective-C support is provided by Stepstone?
11 What kind of Objective-C support is provided by NeXT?
12 What kind of Objective-C support is provided by GNU?
13 What kind of Objective-C support is provided by BPG.
14 What are the newsgroups to read or mailing lists to subscribe to in order
to stay up-to-date on developments for GNU Objective-C?
15 Are there any FTP sites with Objective C code? Where?
16 What class libraries are available for Objective C?
17 So show me a program, a simple example.
18 How can garbage collection be applied to Objective-C?
19 What object encapsulation does Objective-C provide?
(To find a question search on the question number starting a line.)
1 What is Objective-C?
Objective-C is an object oriented computer programming language. It is
a superset of ANSI C and provides classes and message passing similar to
Smalltalk.
2 What is the difference between Objective-C and C?
Objective-C is a superset of ANSI C. It is an object oriented language
that has extensions similar to Smalltalk that are integrated into C
syntax without taking anything away.
It includes a few more keywords and constructs.
New Keywords:
@class
@interface
@implementation
@public
@selector
@protocol
@end
Constructs:
[aList add: anObject]; // send a message to an instance
Types:
id // a generic pointer to an object
[This is nowhere near to what I would like it to be.]
3 What is the difference between Objective-C and C++?
C++ follows the Simula 67 school of OO programming, where Objective-C
follows the Smalltalk school. In C++ the static type of an object
determine whether you can send it a message, in Objective-C the dynamic
type determine it. The Simula 67 school is safer, in that more errors
are detected at compile time. The Smalltalk school is more flexible, as
some valid programs will execute correctly in Smalltalk, where they
would be rejected by Simula 67.
Stepstone's Objective-C allows you to chose between the dynamic and
static binding, GNU and NeXT do not. ANSI C++ allows you to use dynamic
binding, but discourages you from doing so.
In many ways, the difference between C++ and Objective-C is more a
question of mindset than technical barriers. Are you willing to offer
some flexibility for some safety? Advocates for the Simula 67 school
claims that a well designed program doesn't need the extra flexibility
(a lie), while advocates for the Smalltalk school claims that the errors
are no problem in practice (another lie).
Pragmatic differences between Objective-C and C++ include:
C++ has operator overloading. Some consider this to be `syntactic
sugar', and it is, but it can be a quite handy bit of sugar.
C++ has multiple inheritance. There are several ways to `get
around' this in Objective-C (see below).
The added syntax and semantics of C++ is huge, while Objective-C is
C plus just a small number of new features.
4 What exactly is it that makes Objective-C have `classes similar to
Smalltalk', and what are the resulting capabilities of Objective-C?
Objective-C is as close to Smalltalk as a compiled language allows. The
following is a list of the features `taken' from Smalltalk:
* Objective-C is compiled---Smalltalk is only partially compiled. The
current Objective-C implementations are all *much* faster than any
Smalltalk. For example ParcPlace Smalltalk-80/4 is at least 3 times
slower than both the GNU and NeXT Objective-C's. (This was measured
using the Self/Smalltalk benchmark suite available by FTP from
`self.stanford.edu:pub/Self-2.0.1'.)
* You may add or delete methods and classes at runtime. (This is done
via dynamic loading of object code. This is currently supported by
both NeXT and Stepstone Compilers [?].)
* Much of the syntax, i.e. Smalltalk uses method names like
`a:method:name:', as does Objective-C. In Objective-C, the message
sending construct is enclosed in square brackets, like this:
`[anObject aMessage: arg]' whereas Smalltalk uses something like
`anObject aMessage: arg'.
* The basic class hierarchy, that is, having class `Object' in the very
top, and letting most other classes inherit from it.
* Most method names in class object is the same. E.g. `respondsTo:'.
What is called `doesNotUnderstand:' in Smalltalk is called
`doesNotRecognize:' in Objective-C.
* Smalltalk normally uses `doesNotUnderstand:' to implement
forwarding, delegation, proxies etc. In Objective-C, these tasks
are different:
forwarding/delegation: `forward::' can be overridden to
implement forwarding. On the NeXT, `forward::' is even used
when passing to super.
proxies: (Next) An instance of the NXProxy class forwards all
methods and their arguments to the remote object via Mach
messages.
* Objective-C has meta classes mostly like Smalltalk.
* Objective-C does not have class variables like Smalltalk, but pool
variables and globals are easily emulated via static variables.
5 What are the `nice features' of Objective-C?
The possibility to load class definitions and method definitions
(which extend a class) at run time.
Objects are dynamically typed: Typing information is available at
run time. A prime example of this is NeXTSTEP's `-[Application
loadNibSection:owner:]'
Persistence [...].
Remote objects [...].
Delegation and target/action protocols [...].
6 What are some of the common problems of the language and how can I work
around them?
There are some `common problems':
There is no innate multiple inheritance (of course some see this as
a benefit).
To get around it you can create a compound class, i.e. a class
with instance variables that are ids of other objects.
Instances can specifically redirect messages to any combination
of the objects they are compounded of. (It isn't *that* much of
a hassle and you have direct control over the inheritance
logistics.) [Of course, this is not `getting around the problem
of not having multiple inheritance', but just modeling your
world slightly different in such a way that you don't need
multiple inheritance.]
Protocols address the absence of multiple inheritance (MI) to
some extent: Technically, protocols are equivalent to MI for
purely "abstract" classes (see the answer on `Protocols' below).
[How does Delegation fit in here? Delegation is extending a
class' functionality in a way anticipated by the designer of
that class, without the need for subclassing. One can, of
course, be the delegate of several objects of different
classes. ]
There are no class variables.
You can get around this by defining a static variable in the
implementation file, and defining access methods for it. This
is actually a more desirable way of designing a class hierarchy,
because subclasses shouldn't access superclass storage (this
would cause the subclass to break if the superclass was
reimplemented), and allows the subclass to override the storage
(if the classes access all their own variables via methods).
[The question remains what the exact syntax of class variables
should be: Should a class object A be seen as an instance of its
meta-class MA, which has a super class MB being the meta-class
of A's super, B, and, as such, should A have seperate instances
of class variables defined for B? Or not?]
7 What are Protocols?
Protocols are an addition to Objective-C that allows you to organize
related methods into groups that form high-level behaviors. Protocols
are currently available in NeXTSTEP (since 3.0) and GCC (since 2.4).
Protocols address the MI issue. When you design an object with multiple
inheritance, you usually don't want *all* the features of both A and B,
you want feature set X from A and feature set Y from B. If those
features are methods, then encapsulating X and Y in protocols allows you
to say exactly what you want in your new object. Furthermore, if
someone changes objects A or B, that doesn't break your protocols or
your new object. This does not address the question of new instance
variables from A or B, only methods.
Protocols allow you to get type-checking features without sacrificing
dynamic binding. You can say "any object which implements the messages
in Protocol Foo is OK for this use", which is usually what you want -
you're constraining the functionality, not the implementation or the
inheritance.
Protocols give library builders a tool to identify sets of standard
protocols, independent of the class hierarchy. Protocols provide
language support for the reuse of design, whereas classes support the
reuse of code. Well designed protocols can help users of an application
framework when learning or designing new classes. Here is a simple
protocol definition for archiving objects:
@protocol Archiving
-read: (Stream *) stream;
-write: (Stream *) stream;
@end
Once defined, protocols can be referenced in a class interface as
follows:
/* MyClass inherits from Object and conforms to the
Archiving protocol. */
@interface MyClass: Object <Archiving>
@end
Unlike copying methods to/from other class interfaces, any incompatible
change made to the protocol will immediately be recognized by the
compiler (the next time the class is compiled). Protocols also provide
better type checking without compromising the flexibility of untyped,
dynamically bound objects.
MyClass *obj1 = [MyClass new];
// OK: obj1 conforms to the Archiving protocol.
id <Archiving> obj2 = obj1;
// Error: obj1 does not conform to the TargetAction protocol.
id <TargetAction> obj3 = obj1;
Another use of protocols is that you can declare an ID to conform to
some protocol in order to help the compiler to resolve method name
conflicts:
@interface Foo: Object
-(int) type;
@end
@protocol Bar
-(const char *) type;
@end
-blah1: d
{
id t = [d someMethod];
do_something_with ([t type]);
}
-blah2: d
{
id <Bar> t = [d someMethod];
do_something_with ([t type]);
}
In this example, there are two kinds of the `-type' method. In the
method `-blah1:', the compiler doesn't know what return type to expect
from `[t type]', since it has seen both declarations of `-type'. In
method `-blah2:', it knows that `t' conforms to the `Bar' protocol and
thus that `t' implements the `-type' method returning a `const char *'.
8 What is the difference between the NeXTSTEP, Stepstone and GNU CC
versions of Objective-C?
NeXT extended Stepstone's definition of the language to include new
constructs, such as protocols, which are touted to deal with some
aspects of multiple inheritance.
Stepstone supports static _binding_, whereas NeXTSTEP and GNU CC don't.
All implementations do support static _typing_.
Stepstone has a standard set of Foundation class libraries that work
across all supported machines, including NeXTSTEP. NEXTSTEP comes with
its own set of libraries (called `kits'). GNU libobjc.a currently only
includes the `Object' class, though people are busy on a Real library
(see part two of this FAQ (The ClassWare Listing) for details).
The `Object' class of all implementations differ.
NeXTSTEP and GNU CC support Categories, Stepstone doesn't.
NeXT has a native language debugger, Stepstone and GNU don't. [This is
not really true, since NeXT's debugger is gdb, the GNU debugger, and
their extensions are available. I think I've seen them on the Fall 1992
Edu CD. Their extensions haven't appeared in the official FSF
distribution yet.]
NeXTSTEP (from version 3.0) and GCC (from version 2.4) support protocols
and forward declarations of classes, Stepstone currently does not.
9 What written information concerning Objective-C is available?
Books:
Brad J. Cox, Andrew J. Novobilski: Object Oriented Programming: An
Evolutionary Approach. Addison-Wesley Publishing Company, Reading,
Massachusetts, 1991. ISBN: 0-201-54834-8.
abstract: The first book on Objective-C.
Lewis J. Pinson, Richard S. Wiener: Objective-C: Object Oriented
Programming Techniques. Addison-Wesley Publishing Company, Reading,
Massachusetts, 1991. ISBN 0-201-50828-1.
abstract: Includes many examples, discusses both Stepstone's
and NeXT's versions of Objective-C, and the
differences between the two.
Timothy Budd: An Introduction to Object-Oriented Programming.
Addison-Wesley Publishing Company, Reading, Massachusetts.
ISBN 0-201-54709-0.
abstract: An intro to the topic of OOP, as well as a comparison
of C++, Objective-C, Smalltalk, and Object Pascal
Simson L. Garfinkel, Michael K. Mahoney: NeXTSTEP Programming Step
ONE: Object-Oriented Applications. TELOS/Springer-Verlag, 1993
(tel: (800)SPR-INGE).
abstract: It's updated to discuss NeXTSTEP 3.0 features
(Project Builder, new development environment)
but doesn't discuss 3DKit or DBKit.
NeXTSTEP Object Oriented Programming and the Objective C Language.
Addison-Wesley Publishing Company, Reading, Massachusetts, 1993.
ISBN 0-201-63251-9.
abstract: This book describes the Objective-C language as it
is implemented for NeXTSTEP. While clearly targeted
at NeXTSTEP, it is a good first-read to get to learn
Objective-C.
Articles
`Why I need Objective-C', by Christopher Lozinski.
Journal of Object-Oriented Programming (JOOP) September 1991.
Abstract: This article discusses the differences between C++
and Objective-C in great detail and explains why
Objective-C is a better object oriented language.
`Concurrent Object-Oriented C (cooC)', by Rajiv Trehan et. al.
ACM SIGPLAN Notices, Vol. 28, No 2, February 1993.
Abstract: This article discusses cooC, a language based on the
premise that an object not only provides an
encapsulation boundary but should also form a
process boundary. cooC is a superset of
Objective-C.
`Porting NEXTSTEP Applications to Microsoft Windows',
by Christopher Lozinski. NEXTWORLD EXPO Conference Proceedings,
San Francisco, CA, May 25-27, 1993. Updated version of the article
available from the author. Contact lozinski@cup.portal.com
Abstract: This article describes how to develop Objective-C
applications for both Microsoft Windows and
NEXTSTEP.
GNU Documentation
The GNU project needs a free manual describing the Objective-C
language features. Because of its cause, GNU cannot include the
non-free books in the GNU system, but the system needs to come with
documentation.
Anyone who can write good documentation, please think about giving
it to the GNU project. Contact rms@gnu.ai.mit.edu.
10 What kind of Objective-C support is provided by Stepstone?
Compilers and runtime for: Apple Macintosh (running Mac Programmers
Workshop), DEC Stations (ULTRIX), Data General AViiON (DG/UX),
HP9000/300,400,700,800 (HP-UX), IBM RISC System/6000 (AIX), MIPS,
NeXT, PC-AT (MS-DOS), PS/2 (AIX or OS/2), SCO/NCR UNIX SYS V, Sun 3, 4,
SPARCstations (SunOS or Solaris), Silicon Graphics INDIGO and VAX(VMS).
Other ports available by market demands or consulting services.
ICpak101 Foundation Class Library is avaliable on all the above.
ICpak201 GUI Class Library is available on platforms that support
XWindows, Motif, OpenWindows and SunView.
The Stepstone Corporation
(203) 426-1875 - (800) BUY-OBJEct voice / (203) 270-0106 fax
75 Glen Road
Sandy Hook, CT 06482
11 What kind of Objective-C support is provided by NeXT?
The Objective-C compiler and libraries come bundled with the
NEXTSTEP Developer CD. The compiler essentially is GNU CC. For
information on the Kits which are part of NEXTSTEP, see the
ClassWare Listing (part 2 of this FAQ).
Products are:
NEXTSTEP 3.1 for NeXT or Intel Computers
ObjectWare Catalogue (lists available classes, both commercial
and those freely available).
NeXT Computer, Inc.
900 Chesapeake Drive
Redwood City, CA 94063
tel: 800 848 NEXT
fax: 415 780 2801
email: NeXTanswers@NeXT.COM
12 What kind of Objective-C support is provided by GNU?
GNU CC, since version 2, comes with an Objective-C compiler. The
current distribution of GNU CC (version 2.5.0) includes an Objective-C
compiler and runtime library. The latter includes the `Object' class.
Some people are working on GNU libraries, see part two of this FAQ (The
ClassWare Listing) for details.
If you haven't switched to a GNU CC as recent as 2.4 yet, here's one
reason to do so: The new runtime (as of 2.4) is more than 3 times as
fast as the old runtime (pre 2.4) w.r.t. method invocation.
Free Software Foundation
675 Massachusetts Avenue
Cambridge, MA 02139
+1-617-876-3296
General questions about the GNU Project can be asked to
gnu@prep.ai.mit.edu.
GNU CC comes with an Objective-C compiler and runtime library which
includes the Object class.
For information on how to order GNU software on tape, floppy or
cd-rom, check the file etc/DISTRIB in the GNU Emacs distribution, or
e-mail a request to: gnu@prep.ai.mit.edu
GNU software is available from the following sites:
Asia: utsun.s.u-tokyo.ac.jp:/ftpsync/prep,
ftp.cs.titech.ac.jp, cair.kaist.ac.kr:/pub/gnu
Australia: archie.oz.au:/gnu
Europe: ftp.informatik.tu-muenchen.de,
src.doc.ic.ac.uk:/gnu, nic.funet.fi:/pub/gnu,
ugle.unit.no, isy.liu.se, nic.switch.ch:/mirror/gnu,
archive.eu.net, ftp.informatik.rwth-aachen.de:/pub/gnu,
ftp.stacken.kth.se, ftp.win.tue.nl, ftp.denet.dk,
ftp.eunet.ch, irisa.irisa.fr:/pub/gnu
United States: wuarchive.wustl.edu, ftp.cs.widener.edu,
uxc.cso.uiuc.edu, col.hp.com:/mirrors/gnu,
gatekeeper.dec.com:/pub/GNU, ftp.uu.net:/systems/gnu
13 What kind of Objective-C support is provided by BPG.
BPG provides the Borland Extensions to Objective-C which allows the
Objective-C translator to be used with the Borland Compiler, and makes
it easy to develop Objective-C application for Microsoft Windows.
BPG provides the Smalltalk Interface to Objective-C which makes
Objective-C objects look like Smalltalk Objects. It can be used to
build Graphical User Interface on portable Objective-C objects, or to
sell Objective-C libraries to Smalltalk developers.
BPG provides the Objective-C Message Bus which sends Objective-C messages
across heterogeneous computer platforms.
BPG has a library of objects for modelling Objective-C programs. A browser
application has been built on this library. Other potential applications
include adding class variables to Objective-C, adding runtime information
about instance variables, and method argument types, generating object
versions, and eventually building a browser/translator.
Christopher Lozinski
BPG
35032 Maidstone Court
Newark, CA 94560
Tel: (510) 795-6086
fax: (510) 795-8077
email: lozinski@cup.portal.com
14 What are the newsgroups to read or mailing lists to subscribe to in order
to stay up-to-date on developments for GNU Objective-C?
Read comp.lang.objective-c, which is bound to discuss current events.
There is also a mailing list, gnu-objc@gnu.ai.mit.edu, discussing this
very topic. To subscribe to this list, send a mail with your request to
`gnu-objc-request@gnu.ai.mit.edu.'
15 Are there any FTP sites with Objective C code? Where?
NeXTSTEP sites:
sonata.cc.purdue.edu 128.210.15.30
cs.orst.edu 128.193.32.1
ftp.stack.urc.tue.nl 131.155.2.71
ccrma-ftp.stanford.edu 36.49.0.93 (MusicKit)
See also part 2 of this FAQ.
16 What class libraries are available for Objective C?
See the related FAQ file Objective-C/classes.
17 So show me a program, a simple example.
See the related FAQ file Objective-C/sample.
18 How can garbage collection be applied to Objective-C?
Currently, there are two implementations of garbage collection which can
be used in Objective-C programs [that I'm aware of]. Both methods use a
radically different approach.
Garbage Collection in an Uncooperative Environment
This implements garbage collection of chunks of memory obtained
through (its replacement of) malloc(3). It works for C, C++,
Objective-C, etc.
@article{bw88,
title="Garbage Collection in an Uncooperative Environment",
author="Hans J\"urgen Boehm and Mark Weiser",
journal="Software Practice and Experience",
pages=807-820,volume=18,number=9,month=sep,year=1988}
Available as `iesd.auc.dk:/pub/ObjC/gc3.0.tar.z'.
Garbage Collection through Class Abstraction
This implements garbage collection through class abstraction
(and hence is Objective-C specific). Anything to be garbage
collectible must be an object (instance of a subclass of a
specific class) or have such an object for a wrapper.
Available as `ftp.es.ele.tue.nl:/pub/tiggr/tobjc.tar.gz'
Apart from the obvious radical difference, another difference currently
is also noteworthy: The first method automatically protects objects
pointed to from the stack, bss or data segments; the second doesn't.
19 What object encapsulation does Objective-C provide?
Object encapsulation can be discerned at two levels: encapsulation of
instance variables and of methods. In Objective-C, the two are quite
different.
Instance variables:
The keywords @public, @private and @protected are provided to secure
instance variables from prying eyes to some extent.
@public anyone can access any instance variable.
@private only methods belonging to this object's
class or a subclass thereof have access to
the instance variables.
@protected only methods of this class may access the
instance variables. This excludes methods
of a subclass.
If not explicitly set, all instance variables are @private. (Note
that full typing information is available through the runtime,
making all variables @public again. However, this does solve the
problem of instance variables moving in an object because of
recompilation. Such instance variable lookup is performed on
NeXTSTEP by the `loadNibSection...' methods, making it completely
safe.)
Methods:
To the Objective-C runtime, all methods are @public. The programmer
can only show his/her intention of making specific methods not
public by not advertising them in the class' interface. In
addition, so-called private methods can be put in a category with a
special name, like `secret' or `private'.
However, these tricks do not help much if the method is declared
elsewhere, unless one reverts to indicating the object's type at
compile time. And the runtime doesn't care about all this and any
programmer can easily circumvent the tricks described. Thus, all
methods really are always @public.
The early version of this FAQ was compiled by Bill Shirley, with the aid of
many people. The current version is being maintained by Tiggr, aided by a
lot of people, including: Per Abrahamsen, Paul Burchard, Brad Cox,
Christopher Lozinski, Mike Mahoney, Paul Sanchez, Lee Sailer, Bill Shirley,
Subrata Sircar, Ted Slupesky, Richard Stallman and Kersten Krab Thorup,
Any text in between `[' and `]' is a comment. Comments indicate `problems'
with this FAQ, which should be solved. Send your suggestions, additions,
bug reports, comments and fixes to `tiggr@es.ele.tue.nl'.
The information in this file comes AS IS, WITHOUT ANY WARRANTY. You may
use the information contained in this file or distribute this file, as
long as you do not modify it, make money out of it or take the credits.
Technical Details
Message-ID:
<answers_756404773@es.ele.tue.nl>Path:
arc1-ca.pugleaf.net!senator-bedfellow.mit.edu!bloom-beacon.mit.edu!xlink.net!zib-berlin.de!netmbx.de!Germany.EU.net!EU.net!sun4nl!tuegate.tue.nl!krait.es.ele.tue.nl!tiggrShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!senator-bedfellow.mit.edu!bloom-beacon.mit.edu!xlink.net!zib-berlin.de!netmbx.de!Germany.EU.net!EU.net!sun4nl!tuegate.tue.nl!krait.es.ele.tue.nl!tiggr From: tiggr@es.ele.tue.nl (Tiggr) Newsgroups: comp.lang.objective-c,comp.answers,news.answers Subject: comp.lang.objective-c FAQ, part 1/3: Answers Supersedes: <answers_753551527@es.ele.tue.nl> Followup-To: comp.lang.objective-c Date: 20 Dec 1993 16:26:16 GMT Organization: Eindhoven University of Technology, the Netherlands Lines: 630 Approved: news-answers-request@mit.edu Message-ID: <answers_756404773@es.ele.tue.nl> Reply-To: tiggr@es.ele.tue.nl (Tiggr) NNTP-Posting-Host: krait.es.ele.tue.nl Summary: This first part of the comp.lang.objective-c FAQ postings tries to answer all your Objective-C questions. Originator: tiggr@krait.es.ele.tue.nl