Category:Smalltalk: Difference between revisions

Content added Content deleted
Line 93: Line 93:
== Examples ==
== Examples ==
As smalltalk code seems to be hard to read for programmers with a C background, some examples are provided here:
As smalltalk code seems to be hard to read for programmers with a C background, some examples are provided here:
=== Literals ==
<lang smalltalk>
<lang smalltalk>
"a comment - everything in between double quotes"
"a comment - everything in between double quotes"


"/ an end-of-line comment (dialect specific)
"/ an end-of-line comment (dialect specific)

true false "the two Boolean singleton objects"

nil "the UndefinedObject singleton object"


1234 "integer constant; an instance of Integer"
1234 "integer constant; an instance of Integer"
Line 105: Line 110:


1.234 "float constant; an instance of Float"
1.234 "float constant; an instance of Float"

'hello' "string constant; an instance of String"

#'foo' "symbol constant; similar to symbols in lisp, two symbols are identical if they are equal"

$a "character constant"

true false "the two Boolean singletons"

nil "the UndefinedObject singleton object"


(1/7) "fraction constant; an instance of Fraction"
(1/7) "fraction constant; an instance of Fraction"


123s3 "fixed point constant with precision (somewhat dialect specific)
123s3 "fixed point constant with precision (somewhat dialect specific)

'hello' "string constant; an instance of String"

#'foo' "symbol constant; similar to symbols in lisp, two symbols are identical if they are equal"


#(1 true $a 16rFF 1.0 (10 20)) "array literal constant; the last element being another array; an instance of Array"
#(1 true $a 16rFF 1.0 (10 20)) "array literal constant; the last element being another array; an instance of Array"


#[ 10 20 2r1000 16rFE ] "byte-array literal constant; an instance of ByteArray"
#[ 10 20 2r1000 16rFE ] "byte-array literal constant; an instance of ByteArray"

$a "character constant"


[ ... some code ... ] "a block literal; an instance of BlockClosure (dialect specific); the object represents the piece of code which can be passed around and evaluated later (a lambda closure)"
[ ... some code ... ] "a block literal; an instance of BlockClosure (dialect specific); the object represents the piece of code which can be passed around and evaluated later (a lambda closure)"


[:a1 ... :aN | ... some code ...] "a block literal with arguments."
[:a1 ... :aN | ... some code ...] "a block literal with arguments."
</lang>


===Message Sends===
<lang smalltalk>
1000 factorial "send the 'factorial' message to the integer receiver"
1000 factorial "send the 'factorial' message to the integer receiver"


Line 145: Line 149:
...
...
b value "evaluate the block's code"
b value "evaluate the block's code"
</lang>


=== Other ===
<lang smalltalk>
foo := bar "assignment; let foo refer to the object to which bar refers to (at that particular point in time)"
foo := bar "assignment; let foo refer to the object to which bar refers to (at that particular point in time)"


^ a + 1 "return the value of the 'a+1' message send as the return value of the current method."
^ a + 1 "return; the value of the 'a+1' message send is returned as the value of the current method invocation."


'hello' print. 'world' print "statements are separated by a period; just like in english"
'hello' print. 'world' print "statements are separated by a period; just like in english"


|a b c| "local variables; introduces 'a', 'b' and 'c' in the current scope (let-like local bindings)"
</lang>
</lang>