简体   繁体   English

是否可以将[和]保留为以下消息:ifAbsent:如果您不需要完整的块?

[英]Is it OK to leave the [ and ] out for messages like at:ifAbsent: if you don't need a full block?

In Smalltalk (and specifically Pharo/Squeak) I want to know if it is OK to leave out the "[" and "]" for the argument to messages like at:ifAbsent: if you don't need a block, like this; 在Smalltalk(特别是Pharo / Squeak)中,我想知道是否可以省略“[”和“]”作为消息的参数,如:ifAbsent:如果你不需要一个块,就像这样;

^ bookTitles at: bookID ifAbsent: ''.

and

^ books at: bookID ifAbsent: nil.

the code works because (in Pharo/Squeak) Object>>value just returns self. 代码有效,因为(在Pharo / Squeak中)对象>>值只返回self。 But I want to know how accepted this use is or if you should always type the [ and ] even when you don't care if the argument is evaluated quickly or more than once. 但我想知道这种用法是如何接受的,或者你是否应该总是键入[和],即使你不关心参数是否被快速评估或多次评估。

The signature: 签名:

at: key ifAbsent: aBlock 

declares an intention of using a block as a 2nd parameter... But Smalltalk is not a strongly typed language, so, what kind of objects can you pass there? 声明使用块作为第二个参数的意图...但Smalltalk不是强类型语言,那么,你可以通过什么样的对象? any kind that understand the message #value, so, be careful about each particular meaning of #value in each case, but take advantages of polymorphism! 任何理解#value消息的类型,所以,在每种情况下都要注意#value的每个特定含义,但要利用多态性!

This is what Pharo's Code Critics say about similar situations: 这就是Pharo的代码评论家对类似情况的评价:

Non-blocks in special messages: 特殊消息中的非块:

Checks for methods that don't use blocks in the special messages. 检查特殊消息中不使用块的方法。 People new to Smalltalk might write code such as: "aBoolean ifTrue: (self doSomething)" instead of the correct version: "aBoolean ifTrue: [self doSomething]". Smalltalk的新手可能会编写如下代码:“aBoolean ifTrue:(self doSomething)”而不是正确的版本:“aBoolean ifTrue:[self doSomething]”。 Even if these pieces of code could be correct, they cannot be optimized by the compiler. 即使这些代码片段可能是正确的,编译器也无法对它们进行优化。

This rule can be found in Optimization , so you could probably ignore it, but i think it is nicer anyway to use a block. 这个规则可以在优化中找到,所以你可能会忽略它,但我认为无论如何使用块都更好。

Update: 更新:

at:ifAbsent: is not triggered by this rule. at:ifAbsent:不是由此规则触发的。 And it is not optimized by the compiler. 它没有被编译器优化。 So optimization is no reason to use blocks in this case. 因此在这种情况下优化不是使用块的理由。

Not all Smalltalk dialects implement #value on Object out of the box, so your code might not run on other Smalltalk dialects, IF you hand in an object that does not understand #value. 并非所有Smalltalk方言都在开箱即用的对象上实现#value,因此如果您提交了一个不理解#value的对象,您的代码可能无法在其他Smalltalk方言上运行。

It is okay to pass objects of whatever kind as long as you know that what #value does is what you expect, 只要您知道#value的作用是您所期望的,就可以传递任何类型的对象,

Your code may look strange to people who come from other smalltalk dialects or are new to Smalltallk, because they learned that what you pass in here is a Block, but so does sending messages like #join: to a Collection of Strings... 你的代码对于来自其他smalltalk方言的人来说可能看起来很奇怪,或者是Smalltallk的新手,因为他们知道你在这里传递的是一个Block,但是像#join:这样的消息也会发送到一个字符集...

In the end, I'd say don't worry if portability is not a major issue to you. 最后,我要说如果可移植性不是您的主要问题,请不要担心。

I would say that it is not a good idea to leave them out. 我想说将它们排除在外并不是一个好主意。 The argument will be evaluated eagerly if you leave out the parens, and will be sent #value. 如果你遗漏了parens,那么这个论点会被热切地评估,并且会被发送#value。 So if "slef doSomething" has side-effects, that would be bad. 因此,如果“slef doSomething”有副作用,那就太糟糕了。 It could also be bad if #value does something you don't expect eg the perhaps contrived 如果#value做了你不期望的事情,例如也许是人为的,那也可能是坏事

bookTitles at: bookID ifAbsent: 'Missing title' -> 'ISBN-000000' bookTitles at:bookID ifAbsent:'缺少标题' - >'ISBN-000000'

If your code works and you are the only person to view the source, then its ok. 如果您的代码有效并且您是查看源代码的唯一人员,那么可以。 If others are to view the source then I would say a empty block [] would have been more readable. 如果其他人要查看源代码,那么我会说空块[]会更具可读性。 But generally speaking if you really care about bugs its a good idea not to venture outside standard practices because there is no way to guarantee that you wont have any problem. 但一般来说,如果你真的关心错误,最好不要冒险超出标准做法,因为没有办法保证你不会有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM