I am trying to understand the following piece of code in smalltalk
Character extend [
isGraph [
^ (Character space < self) & (self <= $~)
]
visible [
self isGraph ifTrue: [^ '$', self asString]
ifFalse: [^ self asInteger printStringRadix: 16]
]
]
So basically what that code is doing is to extend the functionality of character by adding two new functions to it. IsGraph returns a boolean value, but I don't understand its purpose. How do you interpret (Character space < self) & (self <= $~)?. Somehow the message space is sent to character and that returns something which is compared to self and then self is compared to $~. Can also someone explain the meaning of the symbol ',' in the iftrue block?
welcome to StackOverflow.
First of all the code is adding two new methods and not functions as this is object-oriented programming.
When you send the space
message to the Character
class it will return you an instance if that class which represents the space character. isGraph
probably means "is graphical" because the characters that precede space in the ASCII table do not have a graphical representation (they are NULL, CR, ESC, etc.) as well as the DEL character that follows ~
. Thus with isGraph
, you check whether a character is between space and ~
on ASCII table.
visible
returns a visible representation of a character and relies on isGraph
to decide whether to return the actual character or its integer ASCII representation. The actual character is returned in the Smalltalk's character literal format e.g. $a
is used for character a
, $3
is used for character 3
. Strings are concatenated with the ,
message.
Actually, one of the main points of Smalltalk is understandability. Thus you should be always able to debug a small piece of code or look at the implementations of a message (like ,
in your case). But I suspect that you use something like GNU Smalltalk that lacks many of these features.