Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk (Pharo) How to interface with the user simply

I stumbled upon Smalltalk and further downloaded Pharo, because it was recommended. My first impression is very positive. I wanted to port some simple programs I have written in C++ (my main language), so I can get a feel for Smalltalk.

However, I realized I can't find a simple way to just get some input from the user, e.g. I want to create a console calculator with a REPL loop, but I can't find a way to do this in Pharo. I don't want a gui with buttons, because that is just slow and inconvenient.

Could someone please point out to me how to do simple stuff in Smalltalk, like have the user enter 10 numbers or a command etc.? Do I have to use a different implementation? I'm aiming at std::cin/cout or python's print/input

I know Transcript show:, but that only covers the output portion.

Thanks in advance!

like image 581
smoothware Avatar asked Dec 05 '18 18:12

smoothware


1 Answers

Getting input from the user is easy, but emulating a line-based console not so much. At least it would be more cumbersome than relying on the GUI, which is not really so slow and inconvenient after all.

The closest input equivalent to Transcript show: would be:

UIManager default request: 'Title of the request'

If you mark this snippet and "print it" or "inspect it", you will see a prompt dialog and can type something into it. When you accept the dialog with OK or return, you will get the entered String back as the return value of request:.

Getting ten numbers could be done like this:

(1 to: 10) collect: [:each | (UIManager default request: 'Enter number ', each asString) asNumber ]

The result will be an Array of the entered numbers.

Or, without using collect: and building up the collection yourself:

numbers := OrderedCollection new.
10 timesRepeat:
    [ numbers add: (UIManager default request: 'Enter next number') asNumber ].
numbers do: [:each | Transcript show: each ].

You might also like UIManager default chooseFrom:. Browse the UIManager class and try out the various methods.


Note that Pharo's playground (called workspace in most other Smalltalks) already does what a REPL does, albeit it is not limited to line-based input. You can type Smalltalk expressions, evaluate them and have the results printed there. I am aware that this information does not give you the experience of porting your calculator app from C++. But I guess the way how the I/O works, or rather the interaction with the user, is not the essence of the programs you are trying to port, right? So you might as well write a class Calculator, in which you implement your calculator app, taking a String as input and returning the resulting number, and then invoke it from the playground by evaluating an expression like the following:

Calculator new calculate: '3 + 4'

If you really, really want to stick to console stuff and miss the benefits of the usual Smalltalk IDE and the experience of learning and using it, you might be better off with GNU Smalltalk. I think there are ways to create console applications with Pharo as well, but I would not recommend that to Smalltalk newcomers and I will leave that answer to someone who has already done it once.

like image 51
JayK Avatar answered Sep 19 '22 02:09

JayK