Feeds:
Posts
Comments

Archive for May, 2008

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 sys object is not available in methods.
  • You will find further examples of class definition in "Classy F-Script".

Enjoy!

Advertisements

Read Full Post »

Classy F-Script

Updated on January 7, 2009 to reflect changes in F-Script 2.0 alpha 7.

Work is progressing well on the next alpha of F-Script 2.0. The biggest new feature in alpha 3 will be a nice syntax for creating classes on the fly. Here is an example where we define a Circle class. A circle can be initialized with a color and asked to draw itself.


Circle : NSObject
{
  color

  + circleWithColor:aColor
  {
     ^ self alloc initWithColor:aColor
  }
  
  - (void) draw
  {
     color set.
     (NSBezierPath bezierPathWithOvalInRect:(400<>100 extent:100<>100)) fill
  }
 
  - initWithColor:aColor
  {
     self := super init.
     self ~~ nil ifTrue:
     [
        color := aColor
     ].
     ^self
  }
}

We can then instantiate a circle and draw it:


c := Circle circleWithColor:NSColor greenColor.

c draw

Here is what it looks like in the F-Script console:

This example demonstrates subclassing an Objective-C class (here, NSObject), defining an instance variable (color), a class method and two instance methods. This dynamically creates a native Cocoa class that can then be used from F-Script and Objective-C.

Read Full Post »

As a follow-up to the Mac Developer Roundtable Episode 007, Colin Wheeler explains on Cocoa Samurai how he uses F-Script:

I'm a huge fan of creating experimental projects and playing around with API's and that's one reason I'm a fan of F-Script because then I don't have 30 small projects cluttering up my desktop or a temp directory and so now I do much less tiny projects in Xcode and use F-Script to see how the API's work.
You can start F-Script up in a console and create an app from scratch or just create temp objects and see how they work and play around with Cocoa in a way that's not possible in Xcode without creating loads of small projects.
You can download F-Script from http://www.fscript.org though if you are on Leopard you will need to download and install a special version of FScript Anywhere from http://osiris.laya.com/blog/?p=24

You can learn more on this in "Exploring Cocoa with F-Script", an article that shows the graphical F-Script object browser and some other cool exploration tools in action.

Read Full Post »