Examples
You already saw the classic "hello world" on the home page:
stdout println:'hello world'.
While very simple and a demonstration of basic (Objective-)Smalltalk syntax, it is
slightly atypical in that assumes the stsh
scripting environment, which predefines
the stdout
variable as a ByteStream
that is connected to Unix stdout. More
scripting examples can be found in Scripting.
Methods
An UIAlertViewDelegate
method as used in an educational game ported from Flash:
-<void>alertView:alertView clickedButtonAtIndex:<int>buttonIndex {
self newGame.
self view setNeedsDisplay.
}
Or a method to refresh the subviews:
-<void>refreshViews {
self view subviews do setNeedsDisplay.
}
An application delegate launching method:
-<int>application:app didFinishLaunchingWithOptions:optionsDict {
self checkICloud.
self window setRootViewController: self tabBarController.
self window makeKeyAndVisible.
self startEventTap.
1.
}
A method that deletes a file and fades out the icon representing that file:
-<void>deleteFile:filename {
thumbs := self thumbsView subviews.
viewsToRemove := thumbs selectWhereValueForKey:'filename' isEqual:filename.
aView := viewsToRemove firstObject.
UIView animateWithDuration:0.4
animations: { aView setAlpha: 0.0. }
completion: { aView removeFromSuperview.
UIView animateWithDuration: 0.2
animations: { self thumbsView layoutSubviews. }
completion: { 3 }.
}.
url := self urlForFile:aFilename.
NSFileManager defaultManager removeItemAtURL:url error:nil.
self thumbsView afterDelay:0.4 | setNeedsLayout.
}
Blocks become significantly more readable with Smalltalk syntax.
HOM
As shown in some of the previous examples, Higher Order Messaging (HOM)
is fully supported and helps make code more compact and readable. Take as an example the following code:
self view subviews do setNeedsDisplay.
This code iterates the subviews
of the view controllers view
and sends each one the setNeedsDisplay
message. In Objective-C
the code would probably look as follows:
for ( NSView *aView in [[self view] subviews] ) {
[aView setNeedsDisplay];
}
While the code is not that much longer, it obscures the central point of the code, the setNeedsDisplay
message, with the
machinery for iterating the subviews.
HOM support is made more convenient by the fact that literals default to objects. Here is the code to multiply
an array of numbers by 5:
#( 1, 2, 3, 4, 5 ) collect * 5. -> ( 5 10 15 25 30 )
Summing an array:
#( 1, 2, 3, 4, 5 ) reduce + 0. -> 15
n factorial:
(1 to: n) reduce * 1.
Site
This site was generated by a static site generator based on Objective-S and
Objective-HTTPD. The root site definition is the following method:
dynamic:/ := MPWMainPage pageWithStaticContentNamed:''ObjSTMain'' source:self .
newsplist := rsrc:objst.newsplist value .
items := MPWProductNewsItem collect newsItemsWithDict:newsplist each site:self.
dynamic:/ setNewsItems:items.
dynamic:/ setContentData: self mainPage asData markdownToHtml.
#( ''About'' ''Outlook'' ''Examples'' ''Scripting'' ''URIs'' ''Download'' ) do:[ :name |
data := (self performSelector:name camelCaseString) asData markdownToHtml.
dynamic:/{name} := MPWPlainHtmlContent alloc initWithContentData:data.
].
This code sets the root page to an instance of MPWMainPage
, which does some special evaluation and
formatting of news items. The rest of the pages are filled with HTML created from markdown that is
(at this point) embedded in the code.