Smalltalk (Pharo) 如何简单地与用户交互 [英] Smalltalk (Pharo) How to interface with the user simply

查看:31
本文介绍了Smalltalk (Pharo) 如何简单地与用户交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了 Smalltalk 并进一步下载了 Pharo,因为它被推荐了.我的第一印象是非常积极的.我想移植一些我用 C++(我的主要语言)编写的简单程序,这样我就可以感受一下 Smalltalk.

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.

但是,我意识到我找不到一种简单的方法来从用户那里获取一些输入,例如我想创建一个带有 REPL 循环的控制台计算器,但我在 Pharo 中找不到这样做的方法.我不想要带按钮的 gui,因为那样既慢又不方便.

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.

谁能告诉我如何在 Smalltalk 中做一些简单的事情,比如让用户输入 10 个数字或命令等?我必须使用不同的实现吗?我的目标是 std::cin/cout 或 python 的打印/输入

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

我知道 Transcript show:,但这仅包括输出部分.

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

提前致谢!

推荐答案

从用户那里获取输入很容易,但模拟基于行的控制台就不那么容易了.至少它会比依赖 GUI 更麻烦,毕竟它不是那么慢和不方便.

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.

Transcript show: 等价的最接近的输入是:

The closest input equivalent to Transcript show: would be:

UIManager default request: 'Title of the request'

如果您标记此代码段并打印"或检查",您将看到一个提示对话框并可以在其中输入内容.当您通过 OK 或 return 接受对话框时,您将返回输入的 String 作为 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.

或者,不使用 collect: 并自己构建集合:

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 ].

您可能还喜欢 UIManager default chooseFrom:.浏览 UIManager 类并尝试各种方法.

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

请注意,Pharo 的操场(在大多数其他 Smalltalk 中称为工作区)已经完成了 REPL 所做的工作,尽管它不仅限于基于行的输入.您可以键入 Smalltalk 表达式、计算它们并在那里打印结果.我知道这些信息不能让您体验从 C++ 移植计算器应用程序的经验.但是我想 I/O 的工作方式,或者更确切地说是与用户的交互,并不是您尝试移植的程序的本质,对吗?因此,您不妨编写一个 Calculator 类,在其中实现您的计算器应用程序,将 String 作为输入并返回结果数字,然后通过评估如下所示的表达式从操场中调用它:

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'

如果您真的、真的想坚持使用控制台的东西,而想念通常的 Smalltalk IDE 的好处以及学习和使用它的经验,那么使用 GNU Smalltalk 可能会更好.我认为也有使用 Pharo 创建控制台应用程序的方法,但我不会向 Smalltalk 新手推荐这种方法,我会将这个答案留给已经做过一次的人.

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.

这篇关于Smalltalk (Pharo) 如何简单地与用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆