F-Script can be used on its own or as a sub-language in Objective-C. This allows using F-Script’s object manipulation capacities (e.g., array programming) from Objective-C code without being forced to extend Objective-C itself.
The sub-language term refers to the situation where some code in a given language is embedded in the source code of another program written in another language. For example, Java programmers can use SQL as a sub-language via the JDBC API. A sub-language may be used by a host language without requiring that the host language be extended or modified: the sub-language is integrated using a simple library.
This requires a way of exchanging data between the Objective-C and F-Script, i.e., we must be able to exchange any type of object. Sharing objects between different languages is not a simple matter. There are basically two solutions:
- Providing a run-time that bridges the object models of the two languages
- Using the same object model in the two languages
F-Script adopts the second, straightforward, approach. This allows any object to be exchanged between the two languages with no limitations. Both F-Script and Objective-C can freely share and manipulate the objects, because they are native to both languages.
For example, suppose we have an array of
NSWindow objects in our Objective-C program. We want to put in front of the screen the windows in this array that contain an edited document. Moreover, we want the windows to be “front-sorted” according to the alphabetical order of their titles. Finally, we want to know how many of such “edited” windows are present in our array. We decide to write that little algorithm in F-Script, given how easy this is, and to put it right inside our Objective-C source code.
In F-Script, the parametrized block for performing the specified task would be:
[ :w | w := w at:w isDocumentEdited. (w at:w title sort) reverse orderFront:nil; count]
This defines a block (aka closure or lambda) with a parameter named
w that is used to pass the array of windows. The block uses the standard
isDocumentEdited method provided by the AppKit to determine if a window is associated with an edited document. Such windows are selected, sorted and brought to front. Finally, their number is returned.
Using this code in Objective-C is easy. All we have to do is:
- Add FScript.framework to our project and add the following import directive in our Objective-C source file:
- Put our F-Script code into an
- Turn it into a block by sending the
asBlockmessage to the string
- Execute this block in the same way we would have done in F-Script: by sending it a
value:message, passing our array of windows as argument (in the Objective-C example below, we pass an array that contains all the windows of the application).
NSArray *windows = [[NSApplication sharedApplication] windows]; NSNumber *count = [[@"[ :w | w := w at:w isDocumentEdited. (w at:w title sort) reverse orderFront:nil; count]" asBlock] value:windows];
That’s all for today. Enjoy!