[comp.lang.objective-c] internal subprograms inside a class implementation

jncs@uno.edu (11/26/90)

I am using Objective-C in the NeXT, and I'm learning as I go. I have a simple
question which I hope many of you have run into. I have been trying to define
an "internal" function (ie, not declared in the interface file) inside the
implementation file for a class. In this function I access an instance variable.
The message I get is that the instace variable has not been declared. Thus, it
seems that the scope of instance variables is limited to the body of the
methods. Is there a way to access instance variable in the implementation file?
In general, what is the scope of instance variables in the implementation file?

Thanks
J. Nino

scott@mcs-server.gac.edu (Scott Hess) (11/26/90)

In article <00940412.529FBDA0@uno.edu> jncs@uno.edu writes:
>I am using Objective-C in the NeXT, and I'm learning as I go. I have a
>simple question which I hope many of you have run into. I have been
>trying to define an "internal" function (ie, not declared in the
>interface file) inside the implementation file for a class. In this
>function I access an instance variable.  The message I get is that
>the instace variable has not been declared. Thus, it seems that the
>scope of instance variables is limited to the body of the methods.
>Is there a way to access instance variable in the implementation file?
>In general, what is the scope of instance variables in the
>implementation file?

As Objective-C is implemented on the NeXT, a method is actually converted
into a function of the form:

return-type function_name( id self, SEL _cmd, ...)

That's just a skeleton, and of course is probably totally wrong (this
is gleamed out of a perusal of the StepStone manuals and the NeXT
programmer's Ref. man, neither of which is in front of me at this time).

When you define a function in your code, it's more like:

return-type function_name( ...)

So, how do we find what instance we're working on?  Well, you don't,
so you do not have any decent method of finding the instance variables
for that instance.

What you _can_ do is access them from the method as regular C
structures:

@interface Barf : Object
{
  int intValue;
}
@end

@implementation Barf
void mul( Barf *self, int mulVal)
{
  self->intValue*=mulVal;
}
@end

You might be required to add a line:

@public

before the original declaration of intValue.  I'm not really sure.  @public
makes it accessable outside of the class' methods.  Note that the instance
variable had to be declared as explicitely an instance of the Barf class,
otherwise we cannot tell where the variable belongs in the instance's
structure.

Otherwise, there is a procedure (in NeXT's version, and there's something
similar in the StepStone one) which will get the variable for you
by name.  This is slow, as it requires a bunch of lookup, but it is
probably more generic, also.  Look in the section of the References manual
which describes the Objective-C run-time functions for more info . . .

One last method of doing this would be something like (assuming the same
wrapper):

void mul( id self, int mulVal)
{
  [self setIntValue:[self intValue]*mulVal];
}

Because you _can_ call methods in a regular function.  intValue and
setIntValue are declared as would be expected.  self is an id, now,
because the regular Objective-C run-time stuff can handle the call,
here.
--
scott hess
scott@gac.edu
Independent NeXT Developer	(Stuart)
GAC Undergrad			(Horrid.  Simply Horrid.  I mean the work!)
<I still speak for nobody>