Updated on January 7, 2009 to reflect changes in F-Script 2.0 alpha 7.
The new version of F-Script provides syntax for dynamically creating Cocoa classes. This is great for quickly experimenting, prototyping and using Cocoa interactively. You can type a class definition in the F-script console, hit return and immediately start playing with your new class. This article provides a quick introduction to this easy-to-use and powerful feature. You can experiment with it right now by downloading F-Script 2.0 alpha.
A simple class
In order to define a class, we must give it a name and specify a superclass. Here is an example where we define Buddy as a subclass of NSObject. You can type this code in F-Script and get the Buddy class dynamically created:
Buddy : NSObject {}
As is, this definition is very simple, but not very useful: our new class doesn't define any instance variable or method (though it inherits some from NSObject, its superclass).
Class redefinition
Fortunately, we can dynamically redefine our class. This time, we will specify some instance variables (say firstName and lastName) along with an initialization method and a description method:
Buddy : NSObject
{
firstName lastName
- initWithFirstName:first lastName:last
{
self := super init.
self ~~ nil ifTrue:
[
firstName := first.
lastName := last
].
^ self
}
- description
{
^ 'Hello, I am your buddy ' ++ firstName ++ ' ' ++ lastName
}
}
As you can see, the class definition contains the list of instance variables, followed by the method definitions. self and super have the same meaning as in Objective-C and Smalltalk. The caret (^) is the return instruction.
Using the Buddy class
Let's play with our class. We will instantiate it and assign the newly created instance to a variable that we will then evaluate. The interactive session in the F-Script console looks like:
> john := Buddy alloc initWithFirstName:'John' lastName:'Doe'
> john
Hello, I am your buddy John Doe
Class Methods
As in Objective-C, the name of an instance methods is preceded by a minus sign and the name of a class methods is marked with a plus sign. For example we can add the following class method to our Buddy class:
+ buddyWithFirstName:first lastName:last
{
^ self alloc initWithFirstName:first lastName:last
}
We can then use it as follow:
> mary := Buddy buddyWithFirstName:'Mary' lastName:'Doe'
> mary
Hello, I am your buddy Mary Doe
Temporary local variables
As in blocks, temporary local variables in methods are defined enclosed by vertical bars and separated by spaces. For example, |foo bar| defines two local variables named foo and bar. Such variables are automatically initialized to nil. We can rewrite our description method to show the use of a temporary local variable:
- description
{
|fullName|
fullName := firstName ++ ' ' ++ lastName.
^ 'Hello, I am your buddy ' ++ fullName
}
Browsing
Our newly defined class is automatically registered in the Cocoa runtime. We can use it with all our standard tools. For instance, entering sys browse:john in F-Script will open the graphical object browser on the instance we created earlier.

As usual, class methods will appear when browsing class objects (e.g. typing sys browse:Buddy).

Notes
- This is an alpha version and you are likely to encounter bugs. You will improve your karma by reporting them.
- The
sysobject is not available in methods. - You will find further examples of class definition in "Classy F-Script".
Enjoy!
wow, thanks a lot for the new alpha…looks great!
Karsten
Wow! Awful language! ASM is much more readable…
Very cool. The integration with Cocoa is very powerful. SmallTalk for pragmatists.
Um… is there a way to generate an appropriate digestable header for a C or ObjC program that we might wish to consume an F-Script class?
How very interesting, an Obj-C class whose descendent is an F-Script class whose descendant is an Obj-C class…
Guess whose coming to dinner?
@Dave
No automatic generator, but this should be easy to do manually, by keeping the method declarations from the F-Script code and creating an Objective-C header containing them.
And, BTW, in alpha 6, the runtime will support Objective-C classes inheriting from F-Script classes.