TweetFollow Us on Twitter

About MacApp
Volume Number:3
Issue Number:8
Column Tag:MacApp Applications

How to Think in MacApp

By Howard Katz, British Columbia, Canada

This is very important. You must think in Russian. You cannot think in English and transpose. Do you think you can do that, Mr. Grant?

With those immortal words, our hero, Clint Eastwood, alias Mr. Grant, steals a Russian thought-controlled steath fighter, out-shoots his way from behind the Iron Curtain, and flies to freedom. Learning to think in MacApp is a little like that thought-controlled Jet Fighter. You must think in MacApp. You cannot think in Pascal and transpose. Do you think you can do that Mr. Grant?

Thinking StarTrek in Object Pascal

If you’ve been at all intrigued by what you’ve been reading about MacApp and object-oriented programming, you’re not alone. Apple’s been promoting MacApp heavily, and a number of developers, myself included, have discovered that object-oriented programming is a new and exciting way of doing and thinking about applications. But if you’re even the slightest bit confused by what you’ve read, don’t feel too bad - again, you’re not alone. I had a lot of trouble when I was first starting out (an understatement!), and I’ve talked to other developers who’ve also experienced similar difficulties. Much of my confusion centered not so much on MacApp itself, but rather on the more fundamental language issues introduced by Object Pascal (aka MPW Pascal). If you don’t have a good, solid understanding of what objects are and how to work with them, you won’t have a hope in a hot place of understanding what MacApp is all about.

This article, then, is an attempt to focus on a few of these new language issues, to hopefully cast them in a new light. I don’t think my treatment here is really all that different from what’s been presented in Apple’s documentation, in Kurt Schmucker’s Object-Oriented Programming for Macintosh, or in earlier issues of MacTutor. In some cases, it’s simply a question of emphasis, or of looking at a particular concept or programming construct in a slightly different way.

To make this exposition as “real” as possible, I’m going to assume that we’re writing a hypothetical Star Trek game and use that as a vehicle for my discussion (I personally need to see lots of concrete code before I can understand new concepts; you might be similar). Anyway, my apologies to Gene Roddenberry and Trekkies everywhere for any mistakes; I’m not trying too hard to be accurate (although I am trying to be objective).

As you’re probably aware, the fundamental new programming structure introduced by Object Pascal is the object (if you knew that, a cigar). Objects are simply packages of data, together with the specific code that acts on that data. Objects present a good way of modeling the behavior of a particular programming entity.

In a Star Trek game, for example, a good candidate for such an entity might be one of the many ships that are manipulated during the game. Let’s consider, for example, creating an object that represents a Klingon warship. Such a Klingon object would represent one ship in our game. It would use its data fields to maintain information on its current weapons status, its position, and so forth. The methods belonging to the Klingon object would manipulate this information to enact the specific behavior we expect of Klingon vessels.

Creating this object in our program is going to involve coding statements in at least three different places in the program. First, in an INTERFACE section of our program we’re going to find something like the following:

 TKlingonVessel = OBJECT
 fNumTorpedoes :  INTEGER;
 ... { other relevant fields }
 PROCEDURE TKlingonVessel.LaunchTorpedoes;
 ... { other relevant methods }
 END;{ TKlingonVessel object type }

Notice, first of all, that this is a TYPE declaration, and that somewhere else in our code we can therefore expect to find a corresponding VAR declaration for a variable of this type. In particular, this is a declaration for an object type. This object-type declaration is our first interesting extension of standard Pascal syntax. It shares some of the characteristics of a RECORD type, except, most notably, that standard Pascal records don’t contain procedures as fields. Strange concept number one. Also note that the procedure name LaunchTorpedoes is prefixed by the object-type name, TKlingonVessel.

The naming conventions in the above piece of code, by the way, are just that - conventions. Object type identifiers start with a “T” and data fields start with an “f”. I’ll point out later why these conventions are useful.

Somewhere else in our program we’ll find an IMPLEMENTATION section that contains the actual code for the procedure (ie, method) TKlingonVessel.LaunchTorpedoes. It might look something like the following:

PROCEDURE TKlingonVessel.LaunchTorpedoes;
 IF fNumTorpedoes > 0 THEN 
 fNumTorpedoes := fNumTorpedoes - 1;

The first interesting question I’d like to address is this: given this declaration of an object type and the IMPLEMENTATION of the single procedure it contains (or at least the single one I’ve shown), how do we invoke, or execute, the code for the procedure TKlingonVessel.LaunchTorpedoes?

If we were working in standard Pascal, the question would be so trivial as to be meaningless: you simply invoke the procedure by naming it at some point in your program. In Object Pascal, it’s not quite that simple. In Object Pascal, you can’t execute the code for this method until the object containing it has been created. And we haven’t created the object yet; we’ve simply declared an object TYPE, a template for the object to be.

This is one of the fundamental differences between standard Pascal and Object Pascal: in standard Pascal, code is fixed and immutable - it simply is. In Object Pascal, code has to be created on the fly at runtime before you can use it. Now, that’s a dramatic, though slightly inaccurate statement. It’s close enough to the way things work, however, to be useful.

How do we create the actual TKlingonVessel object and execute its code? The third piece of our program looks something like this:

 aKlingonVessel  :  TKlingonVessel;
 NEW( aKlingonVessel );
 aKlingonVessel.fNumTorpedoes := 10;

Obviously this piece of code is a wee bit strange - it’s unlikely that we’d create a new Klingon object and then immediately ask it to blindly launch a torpedo. I plead pedagogical considerations. At any rate, here’s the VAR statement for the variable I mentioned. This code fragment says that we’re going to create a new object, and that object will be of type TKlingonVessel as declared earlier. An object of this type will contain the data fields and methods that were declared for that object type. The NEW statement then actually creates the object at runtime and makes its fields and methods available for use.

This use of NEW is an extension of the standard Pascal NEW procedure. The compiler recognizes that we’re creating an object and not a standard data structure by the type of the variable that we’re NEWing, in this case aKlingonVessel.

Once we’ve created our object, its data fields become accessible. The statement

 aKlingonVessel.fNumTorpedoes := 10;

initializes the field fNumTorpedoes; prior to this statement, the value of the field was undefined. Note again the RECORD-like syntax used here. Only this time, we’re working with a variable and not an object type: note that the prefix, or qualifier, is changed accordingly.

Finally, we can execute the code of our launch procedure with the statement:


This creation of a new object is known as instantiation, a wonderful term; we have created an instance of this object type. Its data fields are now stuffable; its code is now executable.

To confuse matters just a bit (just when you thought you were getting things under control): the variable aKlingonVessel is not the object itself. Close, but no cigar. The variable aKlingonVessel is an object reference variable, or simply an object reference. Why?

The relationship between an object reference variable and an object is very similar to that between a handle and the handled block it points to. An object actually is a handled block, but with a few important differences from our standard understanding of the term. It floats on the heap, just like a normal handled block, and is just large enough to contain space for its data fields and code (well, almost). The handle itself, or more properly the object reference variable, is exactly four bytes long, as you’d expect for a handle.

OK, I was bending the truth - our object doesn’t actually contain the code for its methods, as I’ve stated. Rather, it contains a pointer that points to where the code actually resides in memory (and who knows, or cares where that is?). That’s why I said earlier that my statement about creating code on the fly at runtime is somewhat inaccurate - the code is already there; we just create the object that contains the pointer to it. Ken Doyle gave a good description of the method-table mechanism that handles this in the December ’86 issue of MacTutor (saving me from having to explain an implementation issue that I don’t fully understand anyway).

Syntactically, while an object-reference variable such as aKlingonVessel acts much like a handle, notice that we don’t have to use Pascal’s caret symbol to dereference it in order to get at the fields of the object it points to. The period separator is sufficient.

There’s one other interesting thing to look at. When we make the statement:


we might say that we’re invoking this method from outside the object. But once that method begins to execute, we are, in a sense, inside the object. I’m talking here about the subsequent code that gets executed by the above line:

IF fNumTorpedoes > 0 THEN BEGIN
 fNumTorpedoes := fNumTorpedoes - 1;

Notice, since we’re now on the inside looking out, that we needn’t qualify the fieldname fNumTorpedoes with the name of the object, aKlingonVessel, or the typename, TKlingonVessel. Either, in fact, would be an error. And here’s one place where naming conventions are useful: the “f” in “fNumTorpedoes” immediately tells us that this is a field belonging to our object, and not something else such as a global variable (in which case it would probably start with a “g,” again by convention). What’s important is that any of the data fields belonging to this object are accessible from within any of its methods, as long as the object exists. This is an extension of Standard Pascal’s scoping rules and has important consequences which we’ll look at later.

The matter of DoLaunch is slightly more involved. Since we’re inside a Klingon vessel object, DoLaunch might be the name of another method belonging to type KlingonVessel (that I haven’t shown), or it might be the name of a standard Pascal procedure that’s not a method at all. Again, once we’re inside an object and executing one of its methods, any other methods that we invoke that belong to that object are not qualified. Finally, there’s a minor variation on the first possibility that we’ll cover when we look at the subject of inheritance.

OK, we’ve now got Klingon vessel objects. More precisely, we’ve got one Klingon vessel object. This represents one ship. In a real Star Trek game, we would probably expect to find numerous Klingons, and there’s nothing to stop us from creating other objects of the same TKlingonVessel type. For example:

 aKlingon1:  TKlingonVessel;
 aKlingon2:  TKlingonVessel;
 NEW( aKlingon1 );
 aKlingon1.fNumTorpedoes := 10;
 NEW( aKlingon2 );
 aKlingon2.fNumTorpedoes := 30;

Now we’ve got two Klingon vessel objects floating in quadrant four, as well as in the heap. They share the same code (there are two pointers to the single method, LaunchTorpedoes), but it’s important to note that they each exist independently of the other one. In particular, their data fields are unique. This shouldn’t be a big surprise if you think about creating two RECORD variables in Pascal that are both based on the same type definition.

At the end of the above sequence of statements, aKlingon1 has 9 torpedoes left, and aKlingon2 has 29 torpedoes remaining.

OK, we’ve now got Klingon objects galore, one for every Klingon vessel in our game. Let’s back up a bit and put the above piece of code in context. The question is: where are these Klingons being created? In other words, who is creating them? Somebody has that responsibility.

In a typical game, we’ll probably have another object whose job it is to mind the board and keep track of turns and other things like that. We might call this the game object and declare it to be of type TGame. Our TGame object will also be responsible for creating all the vessels that are going to appear during the course of the game. This sequence of events (non-Macintosh usage here) is highly typical of the way most object-oriented programs behave at runtime: we initially instantiate one object; it in turn instantiates another; and so on down the line. (If you’re astute, you might well ask at this point what happens if we just keep on instantiating objects, knowing that every instantiation creates a new block in the heap. A very good question. Don’t ask; I’ll come back to this later).

In any event, if we go back and expand the above piece of code just a bit, it’ll look something like this:


 aKlingon1:  TKlingonVessel;
 aKlingon2:  TKlingonVessel;
 NEW( aKlingon1 );
 aKlingon1.fNumTorpedoes := 10;
 NEW( aKlingon2 );
 aKlingon2.fNumTorpedoes := 30;
 ... { other stuff }
END;  { TGame.NewVessel }

All I’ve really done is bracket the code we saw earlier between the name of the game method and an END statement. Again, we’re being somewhat unrealistic for the sake of pedagogy. It’s much more likely that this NewVessel method of our game object would be used to create one Klingon, and not two, at a time, and that we’d invoke it whenever we wanted to create a new one (as indicated by a menu or dialog selection, or whatever). Since these objects differ only in the number of torpedoes we initialize them with (at least according to the limited context I’m showing here), we’d probably pass in a parameter like NumTorpedoes that immediately gets stuffed into the fNumTorpedoes field. In other words:

TGame.NewVessel( NumTorpedoes : INTEGER );
 aKlingon :  TKlingonVessel;
 NEW( aKlingon );
 aKlingon.fNumTorpedoes := NumTorpedoes;
 ... { other stuff }
END;  { TGame.NewVessel }

To be able to keep track of individual Klingons, the TKlingonVessel type would also probably have a field called fID, and we’d increment this field by one for each new ship we added so that each Klingon had a unique number.

Rather than initializing our objects exactly as I’ve shown above, however, it’s a much more common practise to provide each object with its own initialization method, and pass our parameters to the method to let the object initialize its own fields. This occurs throughout MacApp. To wit:

NEW( aKlingon );
aKlingon.IKlingonVessel( NumTorpedoes );


TKlingonVessel.IKlingonVessel( NumTorpedoes:INTEGER);
 fNumTorpedoes := NumTorpedoes;
 ... { other initialization stuff }

What can we say about the name of the method, IKlingonVessel? Again, simply a matter of convention, in which an “I” (obviously standing for “Init”) is prefixed to the object name. OK, that’s a long digression. The main reason I’ve shown the above code is to pose one further query (I love doing that; can’t you tell?).

The question is this: once the delimiting END statement is reached in the NewVessel method, what happens to the objects that were created there? Well, in a word: nothing. They continue to exist in the heap, but there’s no longer any way to reference their fields or methods from outside them. The only way we had of doing so within the NewVessel block was to use our reference variable, aKlingonVessel (or aKlingon1 or aKlingon2, as appropriate). Pascal’s scoping rules say that these variables are local to the method and cease to be once the block is exited. This is a problem, since our game object is likely to want to communicate with them later on.

The answer is to realize again that an object-reference variable is just that: a variable. And the value of a variable is a perfectly good candidate for sticking into one of the data fields of our game object via a Pascal assignment statement. That way, since the fields of the object continue to exist as long as the object itself exists, we’ll be able to get at any “subordinate” objects that are referenced there at any time we like. First, we’ll have to add the necessary reference field to our TGame TYPE declaration:

 fTheKlingon :  TKlingon;
 ...    { other relevant fields }
 PROCEDURE TGame.NewVessel;
 ...  { other relevant methods }
 END;  { TGame object type }

We can then do the following simple assignment in our TGame.NewVessel method:

 aKlingonVessel :  TKlingonVessel;
 NEW( aKlingonVessel );
 fTheKlingon := aKlingonVessel; { <<-- }
 aKlingonVessel.fNumTorpedoes := 10;
 { or fTheKlingon.fNumTorpedoes := 10 }
 { ... etc. }

That’s it! We’ve now established a communcation link, if you will, between our game object and this particular Klingon vessel. No matter what other method of the game object may be executing later on, the game will be able request this Klingon to launch torpedoes or perform any of its other methods by using the fTheKlingon reference field. The syntax for doing so, by the way, is almost identical to what we’ve already seen. For example, if Klingons have a method that allows them to fire a phaser bank (I don’t even know if Klingons have phaser banks!), the game object can request one to do so simply by saying


We can even extend this usage into stranger realms. If our Klingon type has a method that allows it to scan neighboring quadrants for enemy warships and report their location, the game object can ask it to do so by saying

EnemyPosit := fTheKlingon.ReportEnemyPosit;

This is an example of a method that’s actually a function, rather than a procedure. This construct might seem somewhat strange if you haven’t encountered it before. I remember when I was reading the documentation and seeing constructs like this for the first time; there was a lot of head scratching. Hopefully, you’re not as slow as I was.

I’m going to leave it at that for the moment. There is a lot more. And we haven’t even talked about inheritance, overriding, SELF, or a number of other object-oriented subjects. Stay tuned next issue for Romulans, Vulcans, and the other denizens of deep space. Get objective.


Community Search:
MacTech Search:

Software Updates via MacUpdate

iExplorer 4.1.9 - View and transfer file...
iExplorer is an iPhone browser for Mac lets you view the files on your iOS device. By using a drag and drop interface, you can quickly copy files and folders between your Mac and your iPhone or... Read more
PCalc 4.5.3 - Full-featured scientific c...
PCalc is a full-featured, scriptable scientific calculator with support for hexadecimal, octal, and binary calculations, as well as an RPN mode, programmable functions, and an extensive set of unit... Read more
Slack 2.9.0 - Collaborative communicatio...
Slack is a collaborative communication app that simplifies real-time messaging, archiving, and search for modern working teams. Version 2.9.0: Slack now officially, and fully, supports Japanese.... Read more
Microsoft Office 2016 15.40 - Popular pr...
Microsoft Office 2016 - Unmistakably Office, designed for Mac. The new versions of Word, Excel, PowerPoint, Outlook and OneNote provide the best of both worlds for Mac users - the familiar Office... Read more
Apple iOS 11.1.2 - The latest version of...
iOS 11 sets a new standard for what is already the world’s most advanced mobile operating system. It makes iPhone better than before. It makes iPad more capable than ever. And now it opens up both to... Read more
Adobe InCopy CC 2018 - Create...
InCopy CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous InCopy customer). Adobe InCopy CC 2018, ideal for large team projects... Read more
Adobe InDesign CC 2018 - Prof...
InDesign CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous InDesign customer). Adobe InDesign CC 2018 is part of Creative Cloud.... Read more
Tor Browser Bundle 7.0.10 - Anonymize We...
The Tor Browser Bundle is an easy-to-use portable package of Tor, Vidalia, Torbutton, and a Firefox fork preconfigured to work together out of the box. It contains a modified copy of Firefox that... Read more
OmniOutliner Pro 5.2 - Pro version of th...
OmniOutliner Pro is a flexible program for creating, collecting, and organizing information. Give your creativity a kick start by using an application that's actually designed to help you think. It's... Read more
iShowU Instant 1.2.3 - Full-featured scr...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more

Latest Forum Discussions

See All

Lineage II: Revolution guide - tips and...
At long last, Lineage II: Revolution has now come to western shores, bring Netmarble's sweeping MMORPG to mobile devices. It's an addictive, epic experience, but some of the systems in the game can be a bit overwhelming. Here are a few tips to help... | Read more »
A Boy and His Blob (Games)
A Boy and His Blob 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: | Read more »
Fight terrible monsters and collect epic...
Released on Western markets early last month, Dragon Project, created by Japanese developer COLOPL, brings epic monster hunting action to mobile for the very first time. Collect a huge array of weapons and armor, and join up with friends to fight... | Read more »
I Am The Hero (Games)
I Am The Hero 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: I Am The Hero is a pixel art, beat 'em up, fighting game that tells the story of a "Hero" with a glorious but mysterious past.... | Read more »
Kauldron (Music)
Kauldron 1.0 Device: iOS Universal Category: Music Price: $3.99, Version: 1.0 (iTunes) Description: Kauldron is our warmest sounding, punchiest synth yet! A completely new modeling technology, combined with carefully designed... | Read more »
Lineage II: Revolution is mobile’s bigge...
NCSoft’s hit fantasy MMORPG series has just made the leap to mobile with the help of Netmarble in Lineage II: Revolution. With over 1.5 million players having already pre-registered ahead of the game’s launch, Revolution hit the app stores... | Read more »
Swing skilfully in new physics-based pla...
Sometimes it’s the most difficult of obstacles that can be the most rewarding. One game hoping to prove this is OCMO, the new tough but fair platformer from developers Team Ocmo. Primed to set every speedrunner’s pulse racing, as an otherworldly... | Read more »
RPGolf (Games)
RPGolf 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Once upon a time, the kingdom was a land of peace, harmony, and an all-consuming passion for the greatest sport - GOLF. Everyone in the... | Read more »
Everything you need to know about Fire E...
Fire Emblem Heroes is getting its biggest update yet as Nintendo unveiled Book II last night, featuring a whole new set of story missions and yes, collectible heroes. The update's not out just yet, but here's what you can expect when the new... | Read more »
The biggest updates out this week - Nove...
A big game update is always a treat. Multiply that by four and you're having a really good week. Those weeks don't come around very often, but you're in luck. This chilly mid-November is chock full updates for some of your favorite titles, and they... | Read more »

Price Scanner via

Early Black Friday sale: Apple iMacs for up t...
B&H Photo has 27-inch iMacs in stock and on sale for up $130-$150 off MSRP including free shipping. B&H charges sales tax in NY & NJ only: – 27″ 3.8GHz iMac (MNED2LL/A): $2149 $150 off... Read more
Apple restocks refurbished Mac minis starting...
Apple has restocked Certified Refurbished Mac minis starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free: – 1.4GHz Mac mini: $419 $80 off MSRP – 2.6GHz Mac... Read more
Save on 12″ MacBooks, Apple refurbished model...
Apple has Certified Refurbished 2017 12″ Retina MacBooks available for $200-$240 off the cost of new models. Apple will include a standard one-year warranty with each MacBook, and shipping is free.... Read more
Early Holiday sale: 12″ iPad Pros for up to $...
B&H Photo has 12″ iPad Pros on sale today for up to $130 off MSRP. Shipping is free, and B&H collects no sales tax outside NY & NJ: – 12″ 64GB WiFi iPad Pro: $749, save $50 – 12″ 256GB... Read more
Holiday sale prices on Apple 13″ MacBook Pros...
B&H Photo has 2017 13″ MacBook Pros in stock today and on sale for $100-$150 off MSRP, each including free shipping plus NY & NJ sales tax only: – 13-inch 2.3GHz/128GB Space Gray MacBook Pro... Read more
Sale: 13″ MacBook Airs starting at $899, $100...
B&H Photo has 2017 13″ MacBook Airs on sale today for $100 off MSRP including free shipping. B&H charges NY & NJ sales tax only: – 13″ 1.8GHz/128GB MacBook Air (MQD32LL/A): $899, $100 off... Read more
Week’s Best Deal on 13″ MacBook Pros: Apple r...
Apple has a full line of Apple Certified Refurbished 2017 13″ MacBook Pros available for $200-$300 off MSRP. A standard Apple one-year warranty is included with each MacBook, and shipping is free.... Read more
Deal: 15″ 2.6GHz MacBook Pro for $1799 w/free...
B&H Photo has clearance 2016 15″ 2.6GHz Touch Bar MacBook Pros in stock today and available for $600 off original MSRP. Shipping is free, and B&H charges NY & NJ sales tax only: – 15″ 2.... Read more
Black Friday pricing on the 1.4GHz Mac mini....
MacMall has the 1.4GHz Mac mini on sale for $399 including free shipping. Their price is $100 off MSRP (20% off), and it’s the lowest price for available for this model from any reseller. MacMall’s... Read more
Early Black Friday deal: 15″ Apple MacBook Pr...
B&H Photo has 15″ MacBook Pros on sale for up to $200 off MSRP. Shipping is free, and B&H charges sales tax in NY & NJ only: – 15″ 2.8GHz MacBook Pro Space Gray (MPTR2LL/A): $2199, $200... Read more

Jobs Board

*Apple* Solution Consultant - Apple (United...
# Apple Solution Consultant - Rochester, MN Job Number: 113037950 Rochester, MN, Minnesota, United States Posted: 19-Sep-2017 Weekly Hours: 40.00 **Job Summary** Are Read more
Sr. Experience Producer, Today at *Apple* -...
# Sr. Experience Producer, Today at Apple Job Number: 56495251 Santa Clara Valley, California, United States Posted: 23-Jun-2017 Weekly Hours: 40.00 **Job Summary** Read more
AppleCare Support Engineer for *Apple* Medi...
…Summary AppleCare Engineering, Software & Services, is a group that works to represent Apple 's World Wide contact centers and Apple 's customers to groups within Read more
Site Reliability Engineer, *Apple* Pay - Ap...
Job Summary The Apple Pay Site Reliability Engineering Team is hiring for multiple roles focused on the front line customer experience and the back end integration Read more
*Apple* Solutions Consultant - Apple (United...
# Apple Solutions Consultant Job Number: 86078534 Fairless Hills, Pennsylvania, United States Posted: 07-Jul-2017 Weekly Hours: 40.00 **Job Summary** As an Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.