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:
#import <FScript/FScript.h> - Put our F-Script code into an
NSString - 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!
I am learning Cocoa and Objective-C and I accidentally found your website. Am I glad I found out F-Script! You bet. Excellent piece of work. I also read your LINQ and F-Script article, another excellent illustration of power of F-Script. Please keep up the excellent work.
One question. I think I am going to use Cocoa-ObjC framework with OpenBase database. Since I can use FS and Cocoa objects interchangebly, I should be able to use FS with OpenBase as well (ofcourse with its cocoa extensions). Am I correct?
Chris
@Chris. Thanks for your comments!
Yes, you should be able to use the OpenBase Objective-C API with F-Script. Moreover, there is some support in OpenBase for writing stored procedures in F-Script.