1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
|
<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Making Smalltalk with the Penguin LG #51</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<!-- *** BEGIN navbar *** -->
<A HREF="index.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
<A HREF="../index.html"><IMG ALT="[ Front Page ]"
SRC="../gx/homenew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="pramode.html"><IMG ALT="[ Prev ]" SRC="../gx/back2.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom></A>
<A HREF="../faq/index.html"><IMG ALT="[ Linux Gazette FAQ ]"
SRC="./../gx/dennis/faq.gif"WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="vrenios.html"><IMG ALT="[ Next ]" SRC="../gx/fwd.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom ></A>
<!-- *** END navbar *** -->
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Making Smalltalk with the Penguin</font></H1>
<h3>A quick tour of Smalltalk</h3>
<H4>By <a href="mailto:jagwar@magma.ca">Jason Steffler</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<img SRC="gx/steffler/penguinInBalloon.gif" height=230 width=200 ALIGN="right">
<h2>
<a NAME="abstract"></a>Abstract</h2>
Since VisualWorks Non Commercial (VWNC) has been freely released for Linux,
there's been an increased interest in the Linux community about Smalltalk.
The purpose of this article is to give an introduction to Smalltalk for
Linux enthusiasts who aren't familiar with it, and to share some of the
characteristics of this language that endears itself to so many programmers.
There's lots of <a href="#furtherReferences">tutorials and references</a>
to Smalltalk out there already. This article isn't intended to be
a tutorial or reference for OO programming or Smalltalk, but just a quick
tour to whet the palate. General OO knowledge isn't assumed, and
the article can be read standalone or while coding-along.
<p>Much of the examples here apply equally well to all implementations
of Smalltalk. Though all implementations of Smalltalk share the same basic
characteristics, there are differences among them - especially when GUI
code comes into play. There's a number of freely available<sup><a href="#footnotes">1</a></sup>
Smalltalk implementations available for Linux: <a href="http://www.qpsf.edu.au/software/GNU/smalltalk.html">GNU
Smalltalk</a>, <a href="http://www.informatik.uni-stuttgart.de/stx/stx.html">Smalltalk/X</a>,
<a href="http://minnow.cc.gatech.edu/squeak.1">Squeak</a>,
and <a href="http://www.cincom.com/visualworks/downloads.html">VisualWorks
Non Commercial</a><sup><a href="#footnotes">2</a></sup>. Squeak in
particular is doing some really cool stuff lately, but the examples here
are written in VWNC, since this is the flavour that I'm most familiar with.
Also, even though there's a later version available, I'm going to use VWNC
v3.0 for illustrative purposes since that is the version with the most
freely available tools/extensions available.
<p>This article covers some background information on Smalltalk, getting
and installing VWNC3, characteristics of Smalltalk (with examples), and
further references. The characteristics of Smalltalk that are covered here
are that it:
<ul>
<li>
is a pure OO environment, encourages OO programming</li>
<li>
can save exact state of the IDE</li>
<li>
is a literate language</li>
<li>
is incrementally byte-compiled</li>
<li>
is a portable environment; write once, run anywhere (virtual machine/ GUI)</li>
<li>
can inspect and manipulate objects in real time</li>
<li>
has a large degree of reflectiveness (start with running app, extensions)</li>
<li>
has garbage collection, no explicit pointers</li>
</ul>
<h2>
<a NAME="backgroundInfo"></a>1.0 Background Information</h2>
<u>Little known Java/Smalltalk factoid</u>: the original Oak team wanted
to use VisualWorks Smalltalk for their purposes, but ended up inventing
Java when the licensing of VisualWorks at that time proved to be too costly
for them<sup><a href="#footnotes">3</a></sup>.
<p>Smalltalk has been around for quite a while, it originated at the Xerox
Palo Alto Research Center (PARC) in the early 70s, and its original intention
was to provide an environment for children to start learning how to program
in. Accordingly, its syntax is very simple and it's a very mature
environment. Smalltalk's power is rooted in its large <a href="#glossary">class</a>
library, which can be a double-edged sword. You don't have to keep
reinventing the wheel to get any work done, but on the other hand you spend
a lot of time looking for the right wheel to reuse when you're first learning
it<sup><a href="#footnotes">4</a></sup>.
<p>Another double-edged sword is that it's a pure OO environment, and thus
encourages people to make the paradigm shift to OO much more strongly than
other Object Based (OB) languages<sup><a href="#footnotes">5</a></sup>
(more on this later). People who make the shift tend to become very
attached to Smalltalk and find it a very fun, productive<sup><a href="#footnotes">6</a></sup>,
and theoretically pure environment to work in. People who don't make
the shift tend to shun it, and stay in OB languages where it's easier to
get away with writing procedural code in the guise of writing OO code.
(No Dorothy, just because you're using a C++ compiler doesn't mean that
you're writing OO code).
<p>Though many people characterize Smalltalk as only being useful in large,
<a href="#glossary">vertical
markets</a>, Smalltalk is extremely flexible in that it is also used in
very small, <a href="#glossary">horizontal markets</a>. Smalltalk
has been used to write applications<sup><a href="#footnotes">7</a></sup>
for large network management systems, to hospital clinical systems, to
palm pilots, to micro controllers, to embedded firmware.
<p>But that's enough background information, let's get on with some real
examples.
<br>
<h2>
<a NAME="installationStuff"></a>2.0 Installation Stuff</h2>
VWNC3 can be obtained from a <a href="http://www.cincom.com/visualworks/vwv3beta-form.html">free
download site</a>. You need to fill in your name, address, city, state,
zip, and email address to proceed. After doing this, when you click
the <b>I Accept</b> button, a 20,594 KByte download immediately starts,
'vw.exe'. Not the most user-friendly download mechanism in the world,
especially making a Windoze & Linux version both available in a Windoze
self-extracting archive, but it works. Download this to the parent
directory of where you want it to be, and unzip using <b>unzip vw.exe</b>.
The zip file will extract itself into Vwnc3. For myself, I'm running
dual-boot, so I downloaded into my /dos directory and unzipped from there
so I can also use it from Windoze, giving me a base directory of <b>/dos/vwnc3</b>.
(Alternatively, you can specify a VISUALWORKS environment variable with
the path).
<p>To start it up, I like to copy the virtual machine to the image directory,
or to make a symbolic link. Since I have it installed on my dos partition,
I'll make a copy:
<br><b> cp /dos/vwnc3/bin/linux86/visualnc /dos/vwnc3/image/visualnc</b>
<br>I noticed that the virtual machine isn't executable by default, so
let's make it that way:
<br><b> chmod 555 visualnc</b>
<br>Then to start the image, it's necessary to tell the virtual machine
what image to run, do:
<br><b> cd /dos/vwnc3/image</b>
<br><b> visualnc visualnc.im</b>
<br>A license reminder screen pops up every time you start VWNC, click
<b>I
accept</b>, and proceed through it. The window that appears on top
is called the <b><a href="#glossary">Transcript</a></b>, and the window
below it is called a <b><a href="#glossary">Workspace</a></b>. Next,
you need to inform the image where to find its needed system files.
To do this, click on <b>File>Set VisualWorks Home</b> from the Transcript.
In the dialog that appears, enter in the home directory: <b>/dos/vwnc3.</b>
<p>The Transcript and Workspace look like:
<blockquote><img SRC="gx/steffler/transcript01.jpg" height=158 width=421>
<br><b>Fig. 1 - VWNC Transcript</b>
<p><img SRC="gx/steffler/workspace01.jpg" height=308 width=423>
<br><b>Fig. 2 - Default VWNC transcript</b></blockquote>
<p><br>The startup workspace has some useful information that's worth reading,
but it isn't necessary to read it to continue through this article.
<p><u>A note on mice</u>: Smalltalk assumes that you have a three
button mouse. If this isn't the case, you can simulate a three button
mouse on a two button mouse by clicking both the left and right buttons
at the same time.
<h2>
<a NAME="notableCharacteristics"></a>3.0 Some Notable Characteristics of
Smalltalk</h2>
<h3>
<a NAME="savingYourWork"></a>3.1 Saving your work</h3>
Here's another double-edged sword: the great thing is that when you
save your IDE, <b>everything</b> is saved <b>exactly</b> in the state where
you left it. What windows you have open, where they were open, what
was highlighted, the <a href="#glossary">objects</a> that exist, everything.
A snapshot in time is taken and is saved in an image. There's no
need to reload a text file into a text editor, and find where you left
off. The other edge is that if you have a corrupted database connection
or have overwritten some objects you shouldn't have, that's also saved
with the image. So there's a need to be cognizant of the state
of your environment when saving it. Having said that though, you
can save your code separate from the image, and reload it into a clean
image if you do run into problems (a good idea to do anyway). A simple
way to save your code is in a fileout.
<p>Lets give saving a try (might as well save the system file location
you just specified). Move your windows around until they're where you like
them, then save your image, select<b> File>Save As...</b> It's a good idea
to save your image as something other than the virgin image, so if you
run into problems you can always return to the clean image and reload your
code. I'll save the image as <b>testingImage</b>. After saving, try
closing the image <b>File>Exit VisualWorks...>Exit. </b>Then to restart,
be sure to pass the new image name to the virtual machine. Note,
that when you save your image, the date and time are printed on the transcript,
like so:
<blockquote><img SRC="gx/steffler/transcript02.jpg" height=158 width=421>
<br><b>Fig. 3 - Transcript after saving as 'testingImage'</b></blockquote>
There's a lot more I could say here on saving your work (Perm Save As,
filing out, change log, Envy), but I'll digress in the interests of brevity.
<br>
<h3>
<a NAME="everythingIsAnObject"></a>3.2 Everything is an object</h3>
...this was my mantra for the first 6 months when I was learning Smalltalk.
Like most programmers back then, I had a background in procedural programming
and it was very difficult to shake that mindset. Here's where the
infamous paradigm shift comes into play. The Transcript is an object,
the menus on the transcript are objects, the buttons on the transcript
are objects, the Workspace is an object, etc. But before we get into
that, let's start with the venerable 'Hello World' example:
<p><a NAME="ex1"></a><b><u>Ex 1</u>: Hello World</b>
<ul>
<li>
Open a new workspace (click on <b>Tools>Workspace)</b></li>
<li>
In the workspace, type: <b>Transcript cr. Transcript show: 'Hello
World.'</b></li>
<li>
Congratulations, you just wrote your first Smalltalk code. Now let's
see it work:</li>
<li>
Highlight that line of code, middle click and select <b>do it</b> (This
evaluates the Smalltalk code you just wrote)</li>
<li>
You should see the 'Hello World' text be printed in the Transcript:</li>
<br><img SRC="gx/steffler/transcript03.jpg" height=158 width=421>
<br><b>Fig. 4 - Hello World example</b>
<li>
Let's examine the Smalltalk code:</li>
</ul>
<ul>
<table BORDER COLS=2 WIDTH="90%" NOSAVE >
<tr NOSAVE>
<td NOSAVE><b>Transcript cr.</b></td>
<td>This code gets a hold of the Transcript object, and asks it to show
a carriage return on itself.</td>
</tr>
<tr>
<td><b>Transcript show: 'Hello World.'</b></td>
<td>Gets a hold of the Transcript object, and asks it to show 'Hello World'
on itself</td>
</tr>
</table>
<li>
You can also print 'Hello World' to the command line, open a new window
and print 'Hello World' on it, or design a GUI and put 'Hello World' on
it. But again for the sake of brevity, we'll move on.</li>
<li>
<b>cr</b> is a message to Transcript, just as <b>show:</b> is a message
to Transcript too. They're just messages that Transcript knows how
to respond to. They're not part of Smalltalk syntax.</li>
<li>
The above point is important to remember for the non-OO programmers out
there, <i>we send messages to objects to ask them to do something</i>.</li>
</ul>
Realize that you just executed your first Smalltalk code <i>without compiling.
</i>You
didn't have to save your code, compile and link it, and then run it.
"So what, it's an interpretive language" you say. Well, not really.
To be more precise, it's an <i>incrementally byte-compiled language</i>.
I'll come back to this <a href="#incrementalByteCompiling">in a bit</a>.
For now, just be aware that you're coding and executing in Smalltalk seamlessly.
Now, on to the next example:
<p><a NAME="ex2"></a><b><u>Ex 2</u>: Displaying current date</b>
<ul>
<li>
In your opened workspace, type: <b>Date today</b></li>
<li>
Highlight that line of code, middle click and select <b>print it </b>(This
evaluates the Smalltalk code you just wrote, and prints out the result.)</li>
<li>
You should see the current date printed, like:</li>
<br><img SRC="gx/steffler/workspace02.jpg" height=180 width=346>
<br><b>Fig. 5 - Printing Date today</b></ul>
<ul>
<table BORDER COLS=2 WIDTH="90%" NOSAVE >
<tr>
<td><b>Date today</b></td>
<td>Asks the class Date what the date today is</td>
</tr>
</table>
<li>
Ok, let's get a better look at what <b>Date today</b> is evaluating to.
Highlight that line of code (without the date that was printed), middle
click and select <b>inspect it</b>. You should see:</li>
<br><img SRC="gx/steffler/aDate01.jpg" height=116 width=264>
<br><b>Fig. 6 - An inspector on a date</b>
<li>
This is called an <a href="#glossary">inspector</a> (This too is an object
BTW). Inspectors are objects that allow you to have a peek at objects.
If you click on <b>self</b> in the inspector, you'll see a textual representation
of the date:</li>
<br><img SRC="gx/steffler/aDate02.jpg" height=116 width=264>
<br><b>Fig. 7 - Inspector showing textual representation of the date</b>
<li>
Look familiar? That's because in Fig. 5, when you printed the <b>Date
today</b>, you were really printing a <i>textual representation</i> of
the Date object. If you click on the two other attributes of this
date, you'll see that <b>day</b> keeps what day number it is within the
year (1->365, usually), and <b>year </b>keeps what year the date is for.</li>
<li>
All objects have a textual representation, even if it's just the type of
object it is (though that isn't very useful).</li>
<li>
The inspector is a very powerful concept. Realize that you just asked
Date for the current date, and you got a hold of a date object. You
can not only peek around this date, but you can also work with it and modify
it. For example, click on <b>day</b>, and enter in n+1 for what is
there. In Fig 7, Feb 6 is the 37th day of the year, so I'll enter
in 38. Then middle click, and choose <b>accept</b>. You'll
notice that it appears that nothing has changed. But what you've
done is to now set the object to represent the date for the n+1th day of
the year. Now, if you click back to the <b>self</b> on the date,
you'll see it's now showing a date for one day later than what the current
date is:</li>
<br><img SRC="gx/steffler/aDate03.jpg" height=116 width=264>
<br><b>Fig. 8 - Inspector showing date after current date</b></ul>
So, not only can we write code and immediately execute it, but we can grab
hold of objects, and manipulate them directly with immediate effect.
These two abilities is part of what makes Smalltalk such a productive environment.
You can code-n-test in real time. If you don't understand what's going
on, you can just grab an object to see what its state is, and manipulate
it to see how it behaves. After a build, if there's a bug in testing,
you can quickly code in a fix and just load it into the testing environment
- no need to recompile.
<p>Another thing to notice is the <a href="#glossary">literateness</a>
of Smalltalk. To get the date today we just asked <b>Date</b> for
<b>today</b>.
Though Smalltalk is a very literate language, it obviously has to break
down somewhere, for example, we can't ask <b>Date</b> for
<b>tomorrow</b>.
That being said though, the general syntax of Smalltalk is very simple
and easy to read. Keep this in mind while looking through the upcoming
code examples.
<p>Let's move on to the third example of this section then, and get into
the paradigm shift aspect of OO:
<p><a NAME="ex3"></a><b><u>Ex 3</u>: Illustrating the paradigm shift</b>
<ul>
<li>
In your workspace, type: <b>1 + 2 * 3</b></li>
<li>
Now, before you evaluate this code, think about what the answer is going
to be. You're probably thinking 7. Well, highlight the line
of code and <b>print it</b></li>
<li>
You'll notice the answer is 9:</li>
<br><img SRC="gx/steffler/workspace03.jpg" height=180 width=346>
<br><b>Fig. 9 - Result of message send</b>
<li>
It's funny how many non-OO programmers go running in tears after I show
them this. "What! No operator precedence, what a foul language!
I'm going to stick to C++ because I can understand it!" Seriously,
I've had it happen many times.</li>
<li>
However, reality depends on the blinders you're wearing; when I wrote the
answer above, I first thought of writing 9, because I was thinking in an
OO mindset, not a procedural mindset.</li>
<li>
The reason this is the case, is because (repeat after me) <b>everything
is an object</b>. In this example, the numbers 1, 2, and 3 are all
objects. The operators: + and * are just <i>messages</i> that are
being sent to objects. The reasoning is that if + and * are just
messages, why should they have precedence over other types of messages?
All messages of the same type are treated equal in Smalltalk.</li>
<li>
To see for yourself, highlight <b>1</b>, middle click, and choose <b>inspect</b>.
You'll get an inspector on SmallInteger, which is the type of object that
the object 1 is.</li>
<br><img SRC="gx/steffler/aSmallInt01.jpg" height=120 width=231>
<br><b>Fig. 10 - Inspecting the object 1</b>
<li>
So, the code is evaluated as:</li>
<ul>
<li>
(the number 1) is asked to do (+) with (the number 2)</li>
<li>
(the number 1) does this, answers the object: (the number 3)</li>
<li>
(the number 3) is asked to do (*) with (the number 3)</li>
<li>
(the number 3) does this, answers the object: (the number 9)</li>
</ul>
<li>
To get the code to evaluate as a procedural mindset would expect, you can
use brackets: <b>1 + (2 * 3)</b></li>
<li>
"OK, why can't I highlight the date I printed, <b>February 6, 2000</b>,
and inspect that to see the date object?" Good question. The
simple answer is that there are certain types of objects that can be brought
into being (instantiated) by just inspecting a textual representation of
them, and there are certain types of objects that can't. Integers
are one of those types of objects that can be instantiated by inspecting
a textual representation of them.</li>
</ul>
<h2>
<a NAME="incrementalByteCompiling"></a>4.0 Incremental byte compiling</h2>
As I mentioned before, Smalltalk is an incremental byte compiled environment.
Some people describe it as being a cross between compiled and interpretive
languages. What happens when you do or print something, is:
<ol>
<li>
the Smalltalk compiler translates the Smalltalk code into byte codes</li>
<li>
the byte codes are passed to the virtual machine for execution</li>
<li>
the byte codes are executed, and the result is returned</li>
</ol>
Well, this sounds rather interpretive, doesn't it? The difference
is that what we've been doing so far isn't normal Smalltalk programming.
Recall that Workspaces are just a temporary sandbox. Normally, when
you're programming, you accept Smalltalk code (save it in the image), and
the code <i>stays byte-compiled</i>. Normally, when you're programming,
you accept Smalltalk code in <a href="#glossary">methods</a> for a class,
and:
<ol>
<li>
the Smalltalk compiler translates the Smalltalk code into byte codes</li>
<li>
the byte codes are kept with that class</li>
<li>
when a message is sent to a class, the appropriate byte codes for the method
are sent to the virtual machine for execution</li>
<li>
the byte codes are executed, and the result is returned</li>
</ol>
The net result is that every time you make a change to your classes, that
small change is semantically checked, is compiled, and takes
effect immediately. It's pretty cool to make a tiny change, then
see it immediately reflected in your program's behaviour.
<p>Java is similar in the respect that it is byte compiled, but different
in the respect that it isn't <i>incrementally</i> byte compiled.
So when Java programming, you need to recompile all of it (or parts of
it if you're using make), and relink all of it every time you want a change
to take effect.
<br>
<h2>
<a NAME="virtualMachine"></a>5.0 Virtual Machine/Portability</h2>
The virtual machine is just what it sounds like - a virtual computer that
knows how to execute Smalltalk bytecodes. The virtual machine is
usually implemented in C (squeak is the notable exception to this, even
its virtual machine is implemented in Smalltalk - then exported to C programmatically).
It's the virtual machine that allows for such portability across different
machine types. Smalltalk has had the write once, run everywhere paradigm
for <b>decades</b>! To give you a quick taste of this, select <b>File>Settings</b>.
In the Dialog that pops up, select the <b>Look Selection</b> of <b>Macintosh,
</b>then
select <b>Accept.</b>
<blockquote><img SRC="gx/steffler/settings01.jpg" height=365 width=439>
<br><b>Fig. 11- Selecting a Mac look-n-feel</b></blockquote>
Now you'll notice that your entire system is running with a Mac look-n-feel!
The first time I saw this back in '95, it blew me out of my chair.
I had just spent a year doing a very painful port of OpenWindows
to Motif for parts of a C based application. Then, here somebody
showed me how they could 'port' their application from SunOS to Solaris
to MacOS with a click of a button!
<blockquote><img SRC="gx/steffler/transcript04.jpg" height=158 width=421>
<br><b>Fig. 12 - Transcript with Mac look-n-feel</b></blockquote>
Keep in mind that the above window is running on a Linux box! I commonly
use this feature at work when my employer is developing in Windoze, because
I prefer the Motif look-n-feel.
<h2>
<a NAME="reflectiveness"></a><b>6.0 Reflectiveness</b></h2>
Here's where I think the real power of Smalltalk comes to play: in
its <a href="#glossary">reflectiveness</a>. In Smalltalk, 98% of
Smalltalk is written in Smalltalk, which makes it easy to customize, enhance,
or tweak the environment. (Squeak is the notable exception here,
100% of it is written in Smalltalk). So it's very easy to see how
Smalltalk is written, and to extend it for your needs. In fact, this
is the whole basis of development in Smalltalk. You <i>start with
a running application</i>, you add business-specific extensions to it,
strip out the parts you don't need, and then deliver the running application.
Here's a simple example to show how to extend Smalltalk:
<p><a NAME="ex4"></a><b><u>Ex 4</u>: Adding inspect menus to all windows</b>
<ul>
<li>
Remember when I said that the Transcript and default workspace were objects?
We should be able to inspect them then right? Right. However,
it's not that easy in the environment as it comes to grab hold of an arbitrary
window to inspect it. We're going to add a simple extension that
allows for this.</li>
<li>
Open a class browser <b>Browse>All Classes</b></li>
<li>
You now see what is called the class browser, or System Browser.</li>
<br><img SRC="gx/steffler/classBrowser01.jpg" height=352 width=644>
<br><b>Fig. 13 - Empty class browser</b>
<li>
Going through the panes from left to right, top to bottom, you see: <a href="#glossary">categories</a>,
<a href="#glossary">classes</a>,
<a href="#glossary">protocols</a>,
<a href="#glossary">methods</a>,
and method code. Additionally, you'll see two radio buttons: one
sets the browser to look at the instance side of a class, the other sets
the browser to look at the class side. (We're going to do one instance
method and one class method.)</li>
<li>
Middle click on the category pane, and select <b>Find Class...</b>, then
enter <b>StandardSystemController</b> in the dialog that pops up</li>
<li>
After the browser highlights the <b>StandardSystemController</b> class,
click on the <b>menu messages</b> protocol in the protocol pane, and on
the <b>back</b> method in the method pane. You should now see:</li>
<br><img SRC="gx/steffler/classBrowser03.jpg" height=352 width=644>
<br><b>Fig. 14 - Class browser with method selected.</b>
<li>
Briefly, categories are groups of classes, we have the <b>Interface-Framework</b>
category selected. We also have the <b>StandardSystemController</b>
class selected. Protocols are groups of methods, and we currently
have the
<b>menu messages</b> protocol selected. Methods are where
the code is implemented, and we have the <b>back</b> method selected.</li>
<li>
We're going to add a method to the <b>menu messages</b> protocol. To do
this, just highlight all of the current method and delete it. Don't
worry, the method isn't gone and won't be deleted by doing this.
It's just an easy way to create a new method in the currently selected
protocol.</li>
<li>
Then type in the text:</li>
<br><b>inspectView</b>
<p><b> self view model inspect.</b>
<li>
Then to save the method, middle click and choose <b>accept</b>.</li>
<li>
You'll notice that the method you just entered is now showing in the browser.
It's already been compiled and is ready to be executed. You should
see:</li>
<br><img SRC="gx/steffler/classBrowser04.jpg" height=352 width=644>
<br><b>Fig. 15 - Added inspectView method</b>
<li>
Next, we need to add a class side method. Click on the <b>class</b>
radio button in the class browser.</li>
<li>
Then click on the <b>class initialization</b> protocol, and on the <b>flushMenus</b>
method.</li>
<li>
Just as before, delete all of the text for the method, (the <b>flushMenus</b>
method won't be deleted), and type:</li>
<br><b>initializeForAdditions</b>
<br><b> "Initialize the menu."</b>
<br><b> "self initializeForAdditions"</b>
<p><b> ScheduledBlueButtonMenu :=</b>
<br><b> Menu</b>
<br><b> labels: 'relabel as...\refresh\move\resize\front\back\collapse\close\inspect'
withCRs</b>
<br><b> lines: #(1 7 8)</b>
<br><b> values: #( #newLabel #display #move #resize #front
#back #collapse #close #inspectView).</b>
<li>
After you're done typing this, middle click and <b>accept</b> the method.
Now you should see:</li>
<br><img SRC="gx/steffler/classBrowser05.jpg" height=352 width=644>
<br><b>Fig. 16 - Added initializeForAdditions method</b>
<li>
OK, now comes the neat part. Right click over the method code pane,
you should see the window menu:</li>
<br><img SRC="gx/steffler/classBrowserWindowMenu01.jpg" height=192 width=101>
<li>
At this point, the code has been byte-compiled. But, since it's initialization
code, it hasn't been run yet. We can run this code right from the
browser; just highlight the <b>self initializeForAdditions</b> text, middle
click, and <b>do it</b>.</li>
<li>
Now that the code has been run, you should see <b>inspect</b> added to
the bottom of the window menu:</li>
<br><img SRC="gx/steffler/classBrowserWindowMenu02.jpg" height=217 width=101>
<li>
Now we can inspect any window that has this menu window defined on it.
Try it out by inspecting this class browser we've been using. Just
right click, and choose <b>inspect</b>. You'll see an inspector on
the class browser, selecting the <b>className</b> attribute, you'll see
the class there matches the current class selected:</li>
<br><img SRC="gx/steffler/aClassBrowser01.jpg" height=282 width=324></ul>
Note: What we just did here was a base image extension. This is a
type of coding change that should be done and tracked carefully.
<h2>
<a NAME="garbageCollection"></a>7.0 Garbage Collection</h2>
Yea, though I walk through the valley of doomed schedules, I shall fear
no deallocating...
<p>Garbage collection: AKA the sanity saver. Smalltalk has garbage
collection, which means objects that are no longer referenced are cleaned
up to free up memory. I remember pulling out my hair many a time
when programming in C++ trying to find memory leaks. This is because
in C++ it's up to the developer to manage your memory. If you didn't
deallocate the memory you're using, your application would continually
take up more memory until your machine ran out of memory and crashed.
<p>Speaking of machines crashing, you'll note that we never had to do any
pointer manipulation. This is because Smalltalk has no pointers.
(Well, this is technically incorrect. Every variable reference is
actually a pointer to an object, it's just that in Smalltalk the developer
is relieved of the burden of manually maintaining pointers).
<br>
<h2>
<a NAME="summary"></a>8.0 Summary</h2>
I hope that I've been able to give a concise, meaningful tour of Smalltalk
that has been approachable by non OO programmers. I've shown that
Smalltalk:
<ul>
<li>
is a pure OO environment, encourages OO programming</li>
<li>
can save exact state of the IDE</li>
<li>
is a literate language</li>
<li>
is incrementally byte-compiled</li>
<li>
is a portable environment; write once, run anywhere (virtual machine/ GUI)</li>
<li>
can inspect and manipulate objects in real time</li>
<li>
has a large degree of reflectiveness (start with running app, extensions)</li>
<li>
has garbage collection, no explicit pointers</li>
</ul>
There's lots of other cool things I would liked to have touched upon in
this article, but space constraints just don't allow for them:
<ul>
<li>
the online help</li>
<li>
debugger</li>
<li>
numbers - (try printing <b>100000 factorial </b>in a workspace (if you
have the time for crunching - number size is only limited by your memory),
or try inspecting <b>12/7</b>)</li>
<li>
parcels - a code sharing mechanism</li>
<li>
Refactoring Browser - my favourite programming tool</li>
<li>
HotDraw - a reusable draing framework</li>
<li>
testing frameworks - automate your testing</li>
<li>
dynamic changing of widgets at runtime</li>
</ul>
<h2>
<a NAME="glossary"></a>Glossary</h2>
<table BORDER COLS=2 WIDTH="100%" NOSAVE >
<tr NOSAVE>
<td WIDTH="10%" NOSAVE><b>Category</b></td>
<td>A group of classes</td>
</tr>
<tr>
<td><b>Class</b></td>
<td>A type of an object</td>
</tr>
<tr>
<td><b>Horizontal market</b></td>
<td>A market that tends to have a very large audience and has a very small
impact on that audience. Shinkwrapped software addresses a horizontal
market. For example, a word processing package - if it crashes, just reload
your last saved snapshot.</td>
</tr>
<tr>
<td><b>Inspector</b></td>
<td>A GUI type of object that allows you to look at and work with objects.</td>
</tr>
<tr>
<td><b>Literateness</b></td>
<td>A simple definition of literateness is how readible/simple the syntax
of a language is. Literate programming is programming for readability
for the programmer who comes after you.</td>
</tr>
<tr>
<td><b>Method</b></td>
<td>A piece of Smalltalk code for an object.</td>
</tr>
<tr>
<td><b>Object</b></td>
<td>A grouping of related data and operations. It exhibits behaviour
through its operations on its data.</td>
</tr>
<tr>
<td><b>Protocol</b></td>
<td>A group of methods.</td>
</tr>
<tr>
<td><b>Reflectiveness</b></td>
<td>How much of an environment can be manipulated within itself.
In Smalltalk, 98% of Smalltalk is written in Smalltalk, which makes it
easy to customize, enhance, or tweak the environment. (Squeak is
the notable exception here, 100% of it is written in Smalltalk).</td>
</tr>
<tr>
<td><b>Transcript</b></td>
<td>The main window of the IDE, where other windows (browsers, workspaces,
etc) are opened from. Also keeps a running list of system messages.</td>
</tr>
<tr>
<td><b>Workspace</b></td>
<td>A scratchpad where developers can experiment with code.</td>
</tr>
<tr>
<td><b>Vertical market</b></td>
<td>A market that tends to have a very small audience and has a very large
impact on that audience. For example, a network management system
for a telecommunications company - if it crashes the company loses a million
dollars a minute.</td>
</tr>
</table>
<h2>
<a NAME="furtherReferences"></a>Further References</h2>
<ol>
<li>
A <a href="http://max.cs.kzoo.edu/~eatkins/smtlk/smhist.html">history of
Smalltalk</a></li>
<li>
A <a href="http://www.qks.com/Information_Desk/history.asp">brief history
of Smalltalk</a></li>
<li>
<a href="http://www.goodstart.com">GoodStart</a></li>
<li>
WikiWikis:</li>
<ol>
<li>
<a href="http://www.iam.unibe.ch/~scg/cgi-bin/Smalltalk.cgi">Smalltalk</a></li>
<li>
<a href="http://minnow.cc.gatech.edu/squeak.1">Squeak</a></li>
<li>
<a href="http://wiki.cs.uiuc.edu/VisualWorks/">VisualWorks</a></li>
</ol>
<li>
Newsgroup: comp.lang.smalltalk</li>
<li>
Free Smalltalk archives:</li>
<ol>
<li>
<a href="http://st-www.cs.uiuc.edu/">Smalltalk</a> <i>(UIUC - the granddaddy
of Smalltalk archives)</i></li>
<li>
<a href="http://www.squeak.org/">Squeak</a></li>
<li>
<a href="http://www.listserv.gmd.de/archives/vswe-l.html">VSE</a> <i>(in
German)</i></li>
</ol>
<li>
Tutorials:</li>
<ol>
<li>
<a href="http://www.squeak.org/#tutorials">Squeak</a></li>
<li>
<a href="http://cyclone.cs.clemson.edu/~lab428/VW/VWTableOfContents.html">VisualWorks</a></li>
</ol>
<li>
<a href="http://www.panasoft.com/stlinks/">Lots 'o Smalltalk Links</a></li>
<li>
<a href="http://www.dnsmith.com/SmallFAQ/">Dave's Smalltalk FAQ</a> <i>(There's
a number of ones out there, but even though it's dated, this is the best
IMHO. Dave - it'd be great if you ever got time to finish this.)</i></li>
<li>
<a href="http://www.extremeprogramming.org/">eXtreme Programming</a><i>
(Which originated in a Smalltalk project)</i></li>
</ol>
<h2>
<a NAME="footnotes"></a>Footnotes</h2>
<ol>
<li>
Every implementation has different license restrictions. To my knowledge,
only GNU Smalltalk and Squeak have GNU copyleft types of licenses.</li>
<li>
The last two implementations are the newer ones.</li>
<li>
In <a href="http://x37.deja.com/getdoc.xp?AN=564595555&CONTEXT=949863157.89849870&hitnum=0">article</a>:
<i>http://x37.deja.com/getdoc.xp?AN=564595555&CONTEXT=949863157.89849870&hitnum=0</i></li>
<br><i> Author:</i>
<br><i> Eric Clayberg</i>
<br><i> <clayberg@instantiations.com></i>
<p><i> Patrick Logan <patrick@c837917-a.potlnd1.or.home.com></i>
<br><i> wrote in message</i>
<br><i> news:hTN84.453$HT1.6388@news.rdc1.wa.home.com...</i>
<br><i> ></i>
<br><i> > I wish the original Oak team had chosen to adopt Smalltalk</i>
<br><i> > rather than invent Java</i>
<p><i>They tried to, but ParcPlace wanted too much on a per-copy royalty
basis...sigh</i>
<br>
<li>
Bah! Real programmers reinvent the wheel all the time, it puts hair
on your chest! ...sigh, I've run into that far too many times over
the years. More often than not, the wheels that are reinvented tend
to be rather square. Too bad that schools often encourage this mentality
during formal training. [End soapbox]</li>
<li>
This is some loose terminology that shifts depending on who you're speaking
with. For my purposes, an Object based (OB) language is a non-OO
language that has been evolved towards Object-orientedness. (C++,
OO-COBAL, etc). Some people call these bastardized OO languages.</li>
<li>
Some productivity numbers from <a href="http://www.spr.com/library/0langtbl.htm#Smalltalk">Software
Productivity Research</a>. (A sample of ave. source statements/function
point: Smalltalk: 21, C: 128, C++: 53, Visual Basic 5: 29)</li>
<li>
<a href="http://www.goodstart.com/whoswho.html">GoodStart - who's using
Smalltalk</a><i> - http://www.goodstart.com/whoswho.html</i></li>
</ol>
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright © 2000, Jason Steffler<BR>
Published in Issue 51 of <i>Linux Gazette</i>, March 2000</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<!-- P --> <HR> <!-- P -->
<A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue51/steffler.html">
<FONT SIZE="+2">Talkbacks</FONT></A>
<P>
<!-- *** BEGIN navbar *** -->
<A HREF="index.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
<A HREF="../index.html"><IMG ALT="[ Front Page ]"
SRC="../gx/homenew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="pramode.html"><IMG ALT="[ Prev ]" SRC="../gx/back2.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom></A>
<A HREF="../faq/index.html"><IMG ALT="[ Linux Gazette FAQ ]"
SRC="./../gx/dennis/faq.gif"WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
<A HREF="vrenios.html"><IMG ALT="[ Next ]" SRC="../gx/fwd.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom ></A>
<!-- *** END navbar *** -->
</BODY></HTML>
<!--endcut ============================================================-->
|