Search code examples
smalltalkpharo

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


I remember seeing a method somewhere that actually allowed only letters 'Uppercase', 'lowercase', numbers and the underscore in the name, but I can't find it again for the life of me.

Are any other characters allowed?


Solution

  • If you want to check which Characters are allowed in selector names you could use the RefactoringBrowser scanner and evaluate:

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

    the same applies to instance variable names

    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 "
    

    or class variables

    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 "