点 '.' 的用法在 Smalltalk 中
Usage of dot '.' in Smalltalk
.
在Smalltalk中的具体用法是什么?根据我的理解,它是不同语句的分隔符,但如果语句在末尾则可以省略。对吗?
.
是一个语句分隔符,类似于 Pascal
中的 ;
(通常用在行尾)。动机(原因)是英语中的普通句子以.
.
结尾
被must/could省略的地方是:
变量定义
评论
一个语句块或块中的最后一个语句
方法结束时
当你定义一个#selector
或#selector: message
来自 Smalltalk/X-jv 的示例方法:
selectorAsRegistryName: aSelector
"Splits selector into string words with spaces.
For example: itemName becomes 'Item Name'"
| registryName selectorCollection |
registryName := String new.
selectorCollection := aSelector asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded:true.
selectorCollection at: 1 put: selectorCollection copy first asUppercaseFirst. "/ first string must be uppercase too
selectorCollection do: [ :eachString |
registryName := registryName isEmpty ifTrue: [ eachString ]
ifFalse: [ registryName, Character space, eachString ]
].
^ registryName
.
在Smalltalk中的具体用法是什么?根据我的理解,它是不同语句的分隔符,但如果语句在末尾则可以省略。对吗?
.
是一个语句分隔符,类似于 Pascal
中的 ;
(通常用在行尾)。动机(原因)是英语中的普通句子以.
.
被must/could省略的地方是:
变量定义
评论
一个语句块或块中的最后一个语句
方法结束时
当你定义一个
#selector
或#selector: message
来自 Smalltalk/X-jv 的示例方法:
selectorAsRegistryName: aSelector
"Splits selector into string words with spaces.
For example: itemName becomes 'Item Name'"
| registryName selectorCollection |
registryName := String new.
selectorCollection := aSelector asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded:true.
selectorCollection at: 1 put: selectorCollection copy first asUppercaseFirst. "/ first string must be uppercase too
selectorCollection do: [ :eachString |
registryName := registryName isEmpty ifTrue: [ eachString ]
ifFalse: [ registryName, Character space, eachString ]
].
^ registryName