为什么 ifTrue 和 ifFalse 不用 ; 分隔在小话中?

发布于 2024-09-07 22:22:28 字数 324 浏览 3 评论 0原文

a > b
ifTrue:[ 'greater' ]
ifFalse:[ 'less or equal' ]

我的理解是布尔 a > b 收到消息 ifTrue:[ 'greater' ],然后收到消息 ifFalse:[ 'less or equal' ] 符合泛化:

objectInstance selector; selector2

但是这里需要一个分号来表明selector2 的接收者不是(objectInstance 选择器)而是objectInstance。是不是和上面的条件执行一样?

a > b
ifTrue:[ 'greater' ]
ifFalse:[ 'less or equal' ]

My understanding is that Boolean a > b receives the message ifTrue:[ 'greater' ], and then ifFalse:[ 'less or equal' ] complying to the generalization:

objectInstance selector; selector2

But there a semicolon is needed to specify that the receiver of selector2 is not (objectInstance selector) but objectInstance. Is not the same with the above conditional execution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猛虎独行 2024-09-14 22:22:28

该方法的选择器是 Boolean>>ifTrue:ifFalse:,这意味着它是一个一个方法,带有两个参数,而不是两个带有一个参数的方法。

因此,要调用该方法,您可以向其发送带有两个块参数的消息 ifTrue:ifFalse:

请注意,为了方便起见,还有方法 Boolean>>ifFalse:ifTrue:Boolean>>ifTrue:Boolean>>ifFalse:< /代码>。

The selector of the method is Boolean>>ifTrue:ifFalse:, which means it is one method with two parameters, not two methods with one parameter.

Ergo, to invoke the method, you send it the message ifTrue:ifFalse: with two block arguments.

Note that for convenience reasons, there are also methods Boolean>>ifFalse:ifTrue:, Boolean>>ifTrue: and Boolean>>ifFalse:.

jJeQQOZ5 2024-09-14 22:22:28

所有相关的内容都已经说过了,但仅供您娱乐:

如前所述,

rcvr ifTrue:[...] ifFalse:[...]

只有一条消息 #'ifTrue:ifFalse:' 带有 2 个参数,发送到 rcvr 。该表达式的值是来自该消息发送的值。
相比之下:

rcvr ifTrue:[...]; ifFalse:[...]

是 2 个连续消息的级联(#'ifTrue:' 和 #'ifFalse:'),每条消息都有 1 个参数发送到rcvr。表达式的值是上次发送返回的值。

现在有趣的是,布尔值确实理解 ifTrue: / ifFalse: (每个都有 1 个参数),
因此您的代码适用于副作用(评估这些块),但不适用于其价值。
这意味着:

a > b ifTrue:[Transcript showCR:'gt'] ; ifFalse:[Transcript showCR:'le']

生成与: 相同的输出,

a > b ifTrue:[Transcript showCR:'gt'] ifFalse:[Transcript showCR:'le']

但是:

msg := a > b ifTrue:['gt'] ; ifFalse:['le']

将在 msg 中生成与: 不同的值,具体

msg := a > b ifTrue:['gt'] ifFalse:['le']

取决于 ab 的值。尝试 (ab)=(1 2) 与 (ab)=(2 1)...

许多 Smalltalk 初学者的问题是他们将 ifXXX: 视为语法,实际上它是一个发送产生价值的消息。此外,semi 并不是像许多以前学过的语言那样的语句分隔符,而是一个排序消息发送结构。

对于初学者来说这是一个糟糕的陷阱,因为代码似乎适用于某些特定的值组合,而对于其他值组合却会产生有趣的结果。
希望您的单元测试能够涵盖这些;-)

编辑:要查看错误值的来源,请查看布尔值 >> 返回的内容。 ifFalse:真正接收者的方法...

Everything relevant has already been sayd, but just for your amusement:

As already told,

rcvr ifTrue:[...] ifFalse:[...]

is the one and single message #'ifTrue:ifFalse:' with 2 args sent to rcvr. The value of that expression is the one from that message send.
In contrast:

rcvr ifTrue:[...]; ifFalse:[...]

is a cascade of 2 sequential messages (#'ifTrue:' and #'ifFalse:'), each with 1 arg sent to rcvr. The value of the expression is the one returned from the last send.

Now the funny thing is that booleans do understand ifTrue: / ifFalse: (each with 1 arg),
so your code works for the side effect (evaluating those blocks), but not for its value.
This means that:

a > b ifTrue:[Transcript showCR:'gt'] ; ifFalse:[Transcript showCR:'le']

generates the same output as:

a > b ifTrue:[Transcript showCR:'gt'] ifFalse:[Transcript showCR:'le']

but:

msg := a > b ifTrue:['gt'] ; ifFalse:['le']

will generate different values in msg than:

msg := a > b ifTrue:['gt'] ifFalse:['le']

depending on the values of a and b. Try (a b)=(1 2) vs. (a b)=(2 1)...

The problem of many Smalltalk beginners is that they think of ifXXX: as syntax, where it is actually a message send which generates value. Also, the semi is not a statement separator as in many previously learned languages, but a sequencing message send construct.

A bad trap for beginners, because the code seems to work for some particular value combinations, whereas it generates funny results for others.
Let's hope your unit tests cover these ;-)

edit: to see where the bad value comes from, take a look at what is returned by the Boolean >> ifFalse: method for a true receiver...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文