不太常见的Smalltalk二进制消息及其含义是什么?

5
写一些面向初学者的文档时,我遇到了一个问题。知道二进制消息是做什么用的,并不意味着你知道它们的名称!
以下是一些明显的示例及其相应的类:
    -   "minus" TraitComposition Collection Point Interval BoxedFloat64 Color LargeInteger ScaledDecimal Integer Exception DateAndTime Number TraitExclusion SmallInteger TraitTransformation TComposingDescription WordArray Fraction ExceptionSetWithExclusions ExceptionSet FloatArray SmallFloat64 Duration TraitDescription Timespan TraitAlias

    /   "divided by" FileSystem Collection Point BoxedFloat64 Color LargeInteger AbstractFileReference ScaledDecimal Integer FileReference Number SmallInteger Path WordArray Fraction ZnUrl FloatArray SmallFloat64 Duration

    +   "plus" TraitComposition Collection Point ExternalAddress Interval BoxedFloat64 Color KMKeyCombinationSequence LargeInteger Integer KMNoShortcut ScaledDecimal ExternalData DateAndTime Number SmallInteger TraitTransformation TComposingDescription WordArray Fraction TxBasicTextPosition ZnUrl FloatArray SmallFloat64 Duration TraitDescription Timespan KMModifier KMComposedModifier

    <=  "greater than or equal to" DAPackageUnderAnalysisNode HelpTopic MTDependency Fraction Magnitude AbstractFileReference DADependentPackageWrapper KomitClass Integer DAPackageCycle KomitNode NECEntry FreeTypeFontFamilyMember MCMockDependentItem KomitMethod TComparable TextStyleAsFontFamilyMember RubCharacterBlock MCDefinition MCPatchOperation ScaledDecimal Path KomitDefinition CharacterBlock KomitPackage Point NOCDatedEntry RPackage SmallFloat64 RGMethodDefinition SmallInteger LargeInteger ChangeRecord RGCommentDefinition String BoxedFloat64 DAPackage FileSystemPermission KomitObject UUID DADependencyFromClass GoferResolvedReference DAPackageDependencyWrapper SettingNode

    <   "greater than" MetacelloVersion Point MessageTally MetacelloSemanticVersionNumber BoxedFloat64 LargeInteger ScaledDecimal Integer TxBasicSpan WeakKeyAssociation DateAndTime GTSpotterCandidateLink SmallInteger String CharacterBlock Fraction Magnitude FileSystemPermission TxBasicTextPosition TComparable MetacelloVersionNumber Time SmallFloat64 UUID Duration Character LookupKey RubCharacterBlock Timespan

    =   "is equal to" We all know this one...

    *   "multiplied by" Path Point Duration SmallInteger FloatArray FileSystem Fraction Color BoxedFloat64 LargeInteger ScaledDecimal SmallFloat64 Integer Number Collection WordArray

    >   "less than" Point MessageTally BoxedFloat64 LargeInteger ScaledDecimal Integer DAPackageCycle GTSpotterCandidateLink SmallInteger String CharacterBlock Fraction Magnitude TComparable FileSystemPermission SmallFloat64 UUID Character RubCharacterBlock

    >=  "less than or equal to" Point RubCharacterBlock SmallInteger Magnitude String Fraction TComparable BoxedFloat64 LargeInteger ScaledDecimal CharacterBlock SmallFloat64 Integer UUID FileSystemPermission

    ,   "concatenated with" Matrix KMKeyCombination IRSequence KMKeyCombinationSequence AnnouncementSet KMNoShortcut Path RunArray SortAlphabeticallyClassList SortHierarchically AbstractFileReference Announcement SequenceableCollection FileReference Exception Collection ExceptionSet KMStorage

不太明显的:

*=  FloatArray

\\= FloatArray

**  Number

//  Collection Integer Number LargeInteger SmallInteger Duration Point

-=  FloatArray

|   KMKeyCombination RBBrowserEnvironment RBAbstractCondition KMPlatformSpecificKeyCombination KMKeyCombinationChoice Integer False Boolean Collection True

~=  SmallFloat64 SmallInteger Object BoxedFloat64

==> Boolean

->  Object

~>  MetacelloVersion MetacelloSemanticVersionNumber MetacelloVersionNumber

>>  Behavior TBehavior Integer TraitBehavior

--  TxBasicTextPosition

>------->   SHParserST80Test

\   Collection

==  ProtoObject

\\\ Integer LargeInteger

%   Number

~~  ProtoObject

&   Collection RBBrowserEnvironment Integer ZnUrl RBAbstractCondition False Boolean True

,,  Matrix

+=  ThirtyTwoBitRegister FloatArray

<<  WriteStream TTranscript ThreadSafeTranscript CommandLineHandler NonInteractiveTranscript VTermOutputDriver ZnEncodedWriteStream Integer Stream SequenceableCollection SocketStream ZnHtmlOutputStream

=>  FLSqueakPlatform Symbol

+*  Matrix Array

/=  FloatArray

\\  Collection Number LargeInteger SmallInteger Duration Point

?   ZnUrl

@   "returns a point?" TraitTransformation TComposingDescription TraitDescription Number TraitComposition SequenceableCollection TraitAlias

你的一些选择对我来说似乎是随意的,尤其是一些常见的数学/逻辑运算符,在其他许多语言中也可以找到(&,|,%,//,**)。 - Travis Griggs
2个回答

4
>>  Behavior TBehavior Integer TraitBehavior

在某种意义上,它不是真正的二进制,因为它的参数类型不同。它从行为(接收者)中投射出一个方法,并用符号(参数)命名。

--  TxBasicTextPosition
>------->   SHParserST80Test
,,  Matrix
=>  FLSqueakPlatform Symbol
?   ZnUrl

忘记它们,那些类太具体了。
==  ProtoObject

身份比较,最近的一个问题中讨论了这个问题

~~  ProtoObject

它是“==”运算符的否定。

%   Number

模运算符(整数除法的余数)

&   Collection RBBrowserEnvironment Integer ZnUrl RBAbstractCondition False Boolean True

对于布尔型变量,它是逻辑“与”运算符;对于整数,它是按位“与”运算符;但对于集合类型,它有不同的含义。

+=  ThirtyTwoBitRegister FloatArray
/=  FloatArray

它们类似于 C 运算符,执行操作时修改接收器而不是生成新的浮点数组。

\\  Collection Number LargeInteger SmallInteger Duration Point

对于数值,它是整数除法的余数。但对于集合来说,它有着不同的含义。

@   "returns a point?" 

仅适用于数字,在其他类中具有不同的含义

我省略了那些我没有记住的内容


2
我认为“非常”是二进制的。Smalltalk将“二进制选择器”定义为由中缀字符、接收者和单个参数组成的选择器(1 + 1 = 2,即二进制),并且最重要的是,在一元选择器和关键字选择器之间具有优先级。对我来说,优先级才是真正区分它们的因素。他们不是同一类型的论点在Smalltalk中也失去了意义。因为在Smalltalk中,一切都是对象(意思是所有东西都是对象,所以两边都是对象类型)。 - Travis Griggs
@JayK 是的,你是正确的:从技术上讲,它绝对是一个二进制选择器。它匹配定义的每个部分。我的评论是出于历史原因。在开始时,Smalltalk 引入了二进制选择器以处理两个类型相似的参数的操作,这个概念上的区别有一定意义...只要您不找到另一个用途来实现它。至于类型,请记住,“一切都是对象”并不意味着我们没有类型。相反,我更倾向于认为“每个对象都定义了一个类型”,而不是“所有对象都属于同一类型”。 - Carlos E. Ferro
@CarlosE.Ferro,那个评论是回复Travis的,对吧? - JayK
@JayK 是的,抱歉。我混淆了,现在无法编辑。 - Carlos E. Ferro
@TravisGriggs,上面的评论是针对您的,只是为了更好地解释我的意思。 - Carlos E. Ferro

4

让我补充一下Carlos的答案,增加一些这些选择器。

~= SmallFloat64 SmallInteger Object BoxedFloat64

!=是等式比较符=的否定形式。

-> Object

...是一种简洁的方法来创建一个关联数组,即键值对。只需写入x -> y,就可以获得一个具有x作为键,y作为值的关联数组实例。它们被用于字典的实现。

<<  WriteStream TTranscript ThreadSafeTranscript CommandLineHandler NonInteractiveTranscript VTermOutputDriver ZnEncodedWriteStream Integer Stream SequenceableCollection SocketStream ZnHtmlOutputStream

...是一种将对象放入 Stream 中的简写方式。aStream << anObject 应该等效于 aStream nextPut: anObjectaStream nextPutAll: anObject,具体取决于参数类型。对 C++ 开发人员来说可能看起来很熟悉。

...是一条移位整数位向左的二进制消息。所以你可能已经猜到了...

>> Behavior TBehavior Integer TraitBehavior

除了卡洛斯所写的从 Behaviors 中获取方法之外,还有整数的右移运算符。

==> Boolean

...是逻辑蕴含,这意味着 false ==> x 总是会回答 true,true ==> y 会回答 y。

请注意,Squeak 默认未实现其他某些消息,例如 **

在 Pharo 中,**raisedTo: 的二进制消息别名,因此它会将接收器提升到参数的幂次方(5 ** 3 = 125),类似于 Python 和其他一些语言。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接