Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsfeed.cit.cornell.edu!newsstand.cit.cornell.edu!news.kei.com!news.mathworks.com!newsfeed.internetmci.com!news.sprintlink.net!howland.reston.ans.net!ix.netcom.com!netcomsv!uu3news.netcom.com!netcomsv!uu4news.netcom.com!slcgate!servio!servio!aland
From: aland@servio.slc.com (Alan Darlington)
Subject: Re: Beginners question (What's a First Class Object?)
Message-ID: <1995Oct31.193946.24837@slc.com>
Sender: news@slc.com (USENET News)
Nntp-Posting-Host: servio
Organization: GemStone Systems, Inc., Beaverton OR, USA
References: <46qvcu$bie@gatekeeper.iis.ch.swissbank.com> <4721a2$8bb@ixnews2.ix.netcom.com> <4726ii$di1@siufuxsun01.unifr.ch>
Date: Tue, 31 Oct 1995 19:39:46 GMT
Lines: 56

georg.eberle@unifr.ch writes:
> lovejoya@ix.netcom.com (Alan Lovejoy ) wrote:
> 
> >A "first class object" is one that has as many capabilities 
> >as any other object.  In a programming language, this
> >usually means that the object can be created, modified,
> >stored in a variable, passed as an argument to a function,
> >and be computed as the result of an expression or function.
> >In an OOPL, a first class object should also be able to
> >respond to messages.
> 
> This means that in ST all objects are first class objects. And isn't
> that so, that the term "first class object" is a term that also exists
> in truly OO systems like ST? If that is so, which I belive, it can't
> be that what you say it is.
<snip>

Another possible interpretation of a "first class object" is one
that can respond intelligently.  For example, suppose you have a
method that, given a userId, returns (1) true if the userId is
valid, and (2) if valid, the name of the user.  (I know this is
contrived, but I want to keep it simple, and it does represent a
real kind of problem in returning multiple values.)  As a developer,
you must decide whether to return these values grouped in an array
or in an instance of a specialized class designed to hold them.

If you use an array, you might write:

  result := self nameFor: 'JB'.
  result first ifFalse:
    [^ self].   " name not found "
  name := result last.

Given a specialized class, you might write:

  result := self nameFor: 'JB'.
  result isInvalidUserID ifTrue:
    [^ self].   " no comment needed now "
  name := result userName.

Using an array does not give you the ability send meaningful
messages, plus you are dependent on the elements of the array
staying in the same order.  (The first code fragment fails if
#nameFor: decides to add a third element to the result array.)

Using a specialized class makes the result a "first class
object", whose external protocol is (1) much more meaningful
and (2) independent of its internal implementation.

Not all first class objects are really first class.  :-)

  Cheers!
  Alan Darlington
    (standard disclaimer)

  
