Question

|X Y A B C D| 
Y:= 7.
X:= 6.
(X = Y)
    ifTrue: [X := 0]
    ifFalse:[X := 1].
B:=2.
C:=5.
D:=1.
A:= (B squared)*(C-D).
"print both A and X to screen here"

Simple enough little smalltalk example. I'm just curious how I can get this to print X and A as outputs? is there any way to do it w/o having to perform a 'print it' on the top 6 lines and a seperate 'print it' on the bottom 5 lines? if it could print out on just a 'do it' or a single 'print it' please let me know!

Was it helpful?

Solution

You should define what is "printing" and what is X and A.

If "printing" is a result of the "print it" action, then you are talking in general about returning X and A, as "print it" prints the return result of the selected code. This way you have to think about an object which will represent X and A. For this object you can define a printString method or printOn: and get the result printed. Or you can cheat a bit and return a point by doing X@A.

If you are talking about actually printing the thing somewhere then you have to tell more about where do you want to do it. You can print it in Transcript or similar, but there you have to explicitly send a message to the Transcript with what you want to be printed.

Now if you want to use this for "debugging/testing" reasons, it can be easier to go with "inspect it". In your code you can send inspect messages to the objects that you want to look at, and during the execution inspectors will open showing this objects.

Also I encourage you to follow conventions and make your variable names start with lowercase letter.

OTHER TIPS

Smalltalk has no equivalent of print() or println() or the like, since most Smalltalk environments live in a window environment. There are ways to write output to stdout or std error, but this is very dialect specific.

One of the places that somehow replaces stdout in most dialects is a place/stream/window called Transcript, in most dialects this is the window that launches first when your start the IDE.

To write something there you simple do:

Transcript show: 'A=', A asString, ' ; X=', X asString.

(please note that in Smalltalk, Strings and Collections are concatenated with a comma) You can also write a newLine by sending the message cr to the Transcript like so:

Transcript cr.

Does this answer your question?

A hint for further learning/investigation: Transcript is just a Variable that holds a Stream object. show: is a message that writes some String onto that Stream. asString is a method that returns a String representation of an object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top