swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help for a newby


From: Paul E. Johnson
Subject: Re: Help for a newby
Date: Fri, 17 Dec 1999 18:21:15 -0600

David Smith wrote:
> 
> I am just learning Swarm, going through the tutorials.
> 
> In the declarations found in the .h files I find the following:
> 
> FoodSpace *food;
> id <Grid2d> world;
> Bug *reportBug;
> 
> id bugList;
> ...
> etc.
> 
> What is the difference between these declarations? Wouldn't it siginfy
> the same to declare
> id <Grid2d> World;
> like
> Grid2d *world;

The idea here is that Grid2d is a protocol in the Swarm libraries and
what you ought to do is declare it as type id but then give more
specific information to the compiler by adding in the <Grid2d>.  The
protocol declaration in the <> is optional, but if you include it, then
at compile time the compiler might give you an error or warning that
"object X does not respond to Y".  If you don't put that in, the
compiler has no way to check those things, so it shows up as a runtime
error.

The list can be defined like this

  id <List> buglist;
if you want to be more specific about it. 

In the Swarm 1.0 days, when I started, most of the swarm classes were
not presented to us as protocols and we would use declarations like
  Grid2d *world;
As long as there is a Grid2d class to use, that may still work. However,
the protocol interface is the way to go.


If you've been reading the list for a couple of weeks, you may have seen
Marcus Daniels comments about how objective-c is bad for beginning
programmers (maybe all?) because it allows this weak typing, things will
compile if you just declare them as id and don't put in the protocol
details. However, those things cause runtime crashes if an object gets a
message it can't respond to.

If you are totally new in swarm, perhaps you can look at the User guide
we are working on and tell me where the unclear parts are.  I haven't
gotten much feedback, actually

http://lark.cc.ukans.edu/~pauljohn/Swarm/Beta/SwarmUserGuide

in that directory are ps and html versions, last time I looked anyway.

This is a different thing than the SwarmFaq, but sometime things I learn
appear in the FAQ first.

> ?
> 
> I assume that
> id bugList;
> declares a variable, bugList, as a reference to a generic object that may
> need to be typecast later. It appears to be magically cast in the .m file
> with
> buglist = [List create: self];
> 
> David Smith
This is not a cast. It is telling the List object in the Swarm library
to create an instance called buglist in the zone "self".  If we were
being careful about declarations, I think this would be id <List>
bugList;.  The key thing to understand about objective C is that all you
are buying yourself by this inclusion of <List> is compile-time checking
that, somewhere later in the program, the buglist object is not told to
do something outside its set of methods.  As long as you don't later ask
a buglist to do anything impossible, it makes absolutely no difference
whether you put in the <List> or not.

Because many programs compile and run sometimes, people have trouble
knowing exactly what is going wrong sometimes. That's why Marcus and
others are working so hard to make the Swarm java interface work,
because Java is much more tightly typed, you can't get away with just
saying something is an "id" and leaving it to the runtime.   

I'm waxing on like this because it took me a long time to see the
point.  I think the programmer types in here find it completely obvious.
Not me.  Anyway, welcome to the group.

-- 
Paul E. Johnson                         email: address@hidden
Dept. of Political Science              http://lark.cc.ukans.edu/~pauljohn
University of Kansas                    Office: (785) 864-9086
Lawrence, Kansas 66045                  FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]