Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Smalltalk
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Literals=== The following examples illustrate the most common objects which can be written as literal values in Smalltalk-80 methods. Numbers. The following list illustrates some of the possibilities. <syntaxhighlight lang="smalltalk"> 42 -42 123.45 1.2345e2 2r10010010 16rA000 </syntaxhighlight> The last two entries are a binary and a hexadecimal number, respectively. The number before the 'r' is the [[radix]] or base. The base does not have to be a power of two; for example 36rSMALLTALK is a valid number equal to 80738163270632 decimal. Characters are written by preceding them with a dollar sign: <syntaxhighlight lang="smalltalk">$A</syntaxhighlight> Strings are sequences of characters enclosed in single quotes: <syntaxhighlight lang="smalltalk">'Hello, world!'</syntaxhighlight> To include a quote in a string, escape it using a second quote: <syntaxhighlight lang="smalltalk">'I said, ''Hello, world!'' to them.'</syntaxhighlight> Double quotes do not need escaping, since single quotes delimit a string: <syntaxhighlight lang="smalltalk">'I said, "Hello, world!" to them.'</syntaxhighlight> Two equal strings (strings are equal if they contain all the same characters) can be different objects residing in different places in memory. In addition to strings, Smalltalk has a class of character sequence objects named Symbol. Symbols are guaranteed to be unique—there can be no two equal symbols which are different objects. Because of that, symbols are very cheap to compare and are often used for language artifacts such as message selectors (see below). Symbols are written as # followed by a [[string literal]]. For example: <syntaxhighlight lang="smalltalk">#'foo'</syntaxhighlight> If the sequence does not include whitespace or punctuation characters, this can also be written as: <syntaxhighlight lang="smalltalk">#foo</syntaxhighlight> Arrays: <syntaxhighlight lang="smalltalk">#(1 2 3 4)</syntaxhighlight> defines an array of four integers. <syntaxhighlight lang="smalltalk">#((1 2 3 4) [1 2 3 4] 'four' 4.0 #four)</syntaxhighlight> defines a seven element array whose first element is a literal array, second element a byte array, third element the string 'four', and so on. Many implementations support the following literal syntax for ByteArrays: <syntaxhighlight lang="smalltalk">#[1 2 3 4]</syntaxhighlight> defines a ByteArray of four integers. And last but not least, blocks ([[anonymous function]] literals) <syntaxhighlight lang="smalltalk">[... Some smalltalk code...]</syntaxhighlight> The following takes two arguments and compares any two objects which can understand "less than", for example, numbers, and strings<syntaxhighlight lang="smalltalk">[:a :b| a < b]</syntaxhighlight> Blocks are explained in detail further in the text. Many Smalltalk dialects implement additional syntaxes for other objects, but the ones above are the essentials supported by all.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)