Symbol (programming) - Support - Smalltalk - Examples

Examples

The following is a simple example of a symbol literal in Smalltalk:

my_symbol := #'an identifier' " Symbol literal " my_symbol := #a " Technically, this is a selector literal. In most implementations, " " selectors are symbols, so this is also a symbol literal "

Strings can be coerced into symbols, vice versa:

my_symbol := 'Hello, world!' asSymbol " => #'Hello, world!' " my_string := #hello: asString " => 'hello:' "

Symbols conform to the symbol protocol, and their class is called Symbol in most implementations:

my_symbol := #hello_world my_symbol class " => Symbol "

Symbols are commonly used to dynamically send messages to (call methods on) objects:

" same as 'foo' at: 2 " 'foo' perform: #at: with: 2 " => $o "

Read more about this topic:  Symbol (programming), Support, Smalltalk

Famous quotes containing the word examples:

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)