Smalltalk 实例变量名称和方法中允许使用哪些特殊字符?

What special Characters are allowed in Smalltalk Instance Variable Names and Methods?

我记得在某处看到一个方法实际上只允许名称中的字母 'Uppercase'、'lowercase'、数字和下划线,但我再也找不到它了。

是否允许使用其他字符?

如果您想检查选择器名称中允许使用哪些字符,您可以使用 RefactoringBrowser 扫描器并评估:

RBScanner isSelector: 'invalid@Selector'.
RBScanner isSelector: 'ValidSelector123_test'.
RBScanner isSelector: '111selector123_test'.

同样适用于实例变量名

RBCondition checkInstanceVariableName: 'validInstVar' in: UndefinedObject.
" true, valid instance variable name "
RBCondition checkInstanceVariableName: 'super' in: UndefinedObject.
" false, super is a reserved word in Smalltalk "
RBCondition checkInstanceVariableName: '' in: UndefinedObject.
" false, empty instance variables are not allowed "
RBCondition checkInstanceVariableName: 'Invalid' in: UndefinedObject.
" false, instance variable must start with lowercase character "

或class变量

RBCondition checkClassVarName: 'invalidClassVar' in: UndefinedObject.
" false, because class variables must start with uppercase "
RBCondition checkClassVarName: 'super' in: UndefinedObject.
" false, the same "
RBCondition checkClassVarName: '' in: UndefinedObject.
" false, empty Class variables are not allowed "
RBCondition checkClassVarName: 'Valid' in: UndefinedObject.
" true, a valid class variable "

虽然 OP 很可能意味着 "what is allowed in the regular parsable syntax",但我认为指出 "Smalltalk Textual Language" 只是到达对象模型的中间文本表示形式是有启发性的。如果您愿意发挥创造力,您实际上可以做一些以其简单解析为目标的语法难以支持的事情。

例如,您可以拥有以数字开头的方法:

Object methodDictionary at: #1a put: (Object >> #yourself) copy.
Object new perform: #1a

为什么有以数字开头的字符串,而您可以直接使用数字?

Object methodDictionary at: 42 put: (Object >> #yourself) copy.
Object new perform: 42

或者只是空字符串怎么样?

Object methodDictionary at: #'' put: (Object >> #yourself) copy.
Object new perform: #''

您的浏览器可能会也可能不会妥善处理添加到您系统中的这些方法。

可以对实例变量进行类似的操作。