views:

1207

answers:

10

I'm trying to learn Smalltalk by doing, so I'm getting a grip on the syntax and style by buiding a simple "Matrix" class.

First of all, I'd be grateful if linked to a good Smalltak tutorial (although this is totally optional), preferably one that doesn't involve using the GUIs (I'd rather type my .sts than fish around the hierarchy explorer to put the methods in their place).

Then, on TDD: For this project I'm calling gst-sunit -f matrix.st -f matrix-test.st BaseMatrixTests, and there's bound to be a better way than that. Is there?

And finally, on asserts: I'm trying to write a method and put asserts within, eg.:

Matrix>>multiplyBy: anotherMatrix [
    [ self isNotEmpty ] assert.
    "Do Multiplication"
    [ result dimensions = (self height)@(anotherMatrix width) ] assert.
]

How can I do that kind of asserts?

Edit: Questions marked explicitly.

+3  A: 

Other than a request for a tutorial, I don't see a question here. Could you clarify what it is you want to know?

A good list of resources for a beginner can be found in this question: http://stackoverflow.com/questions/481976/is-there-a-dive-into-smalltalk-book

Steve Rowe
+8  A: 

I hate to say it ('cause you clearly indicated you don't want to hear it), but get into the IDE. Trying to get your head around smalltalk without using the IDE is like going to Paris and eating at McDonalds. Sure, you're in Paris, but you aren't really exposing yourself to what it's all about.

The key thing about smalltalk is that it's all objects. All the way down (integers and characters are objects) and all the way up (classes, methods, the browsers, the IDE itself) are all objects. If you insist on fighting it you'll have about as much luck as someone wanting to learn how to write C by dragging and dropping things.

MarkusQ
+1 for the Paris/McDonalds thing! :-)
André
+11  A: 

Okay, several pieces here.

First, I agree with markusQ, although I completely sympathize: I'd rather be able to write my code in EMACS directly. Bt one of the things about Smalltalk is that it really is very unforgiving of people who don't want to do things the Smalltalk Way. In this case, the Smalltalk Way is to use the browsers.

Second, part of the reason that this is the Smalltalk Way is that Smalltalk is, in a lot of ways, not like other languages. There is really, for all practical purposes, no way to make a "separate" Smalltalk executable: all you can do is make an image of Smalltalk with some relatively small fragments of your own code added in. When you write code using an external editor, as with the syntax you show, you're literally just hand typing an import/export format that is somewhat easier to hand type than XML. But only somewhat.

The moral is, do it the Smalltalk way, with the browsers.

There are some fairly good tutorials for Smalltalk about. I usually use Squeak, so the ones I've seen are using Squeak, as here.

On TDD, you're in luck because Smalltalk was the first place to get xUnit. For Smalltalk, it's called SUnit, and there's a good tutorial here.

You're using assertions there in what appears to be basically a "design by contract" approach. There has been work done on adding design by contract to Smalltalk, as here. For simple assertions, you can add code as in this SO question.

assert: aBlock 
    "Throw an assertion error if aBlock does not evaluates to true."
    aBlock value
        ifFalse: [AssertionFailure signal: 'Assertion failed']
Charlie Martin
+6  A: 

If you downloaded Cincom Smalltalk Non-Commercial, there are a number of online tutorials. Start here:

http://www.cincomsmalltalk.com/userblogs/cincom/blogView?content=tutorials

If you downloaded Squeak, start here:

http://wiki.squeak.org/squeak/792

And yes, you really do need to use the IDE to work effectively with Smalltalk.

On testing, load SUnit. In Cincom Smalltalk, it's a loadable component; I've covered loading (and using) it in the video tutorials linked above. I'm not entirely sure how to load it for Squeak, or whether it's part of the base there, but it's certainly available for it.

jarober
+1  A: 

So, about asserts, Squeak Smalltalk already brings Object>>assert: So, I suppose you can do:

self assert: self isNotEmpty.
self assert: result dimensions equal: (self height)@(anotherMatrix width)

If you are using GNU smalltalk, this might answer how to do assertions there: http://stackoverflow.com/questions/665455/smalltalk-and-assertions

Niko

nes1983
A: 

link text

A: 

Smalltalk Primer

+1  A: 

Regarding asserts, please look at the other question recently posted.

Regarding TDD, yes, calling gst-sunit -f matrix.st -f matrix-test.st BaseMatrixTests is sort of the best way. Everything else just builds on that, for example these could be thee alternatives:

  • make all TestCase subclasses for your package inherit from a phony subclass so that you can say AllMatrixTests* on gst-sunit's command line (when you add more tests).
  • file in matrix.st from matrix-test.st, thus eliminating one -f option.
  • create a Makefile and package.xml file to create a .star file for your package, as described here. Then you can do just gst-sunit -pMatrix.
Paolo Bonzini
+1  A: 

It has been suggested above to add #assert: to Object, but rather I'd add #assert to BlockClosure (or whatever [] class is in GNU Smalltalk).

assert
    this value ifFalse: [AssertionFailure signal: 'Assertion failed']

and thus use as in

[ value notNil ] assert.
[ value > 0 ] assert.
[ list isEmpty not ] assert.

etcetera.

Adrian
A: 

http://stores.lulu.com/store.php?fAcctID=2748699 (Also you will find the link to download the pdf)

Germán Arduino