Archive for August, 2008

Become a German Xcoder!

« Become an Xcoder », the book I wrote with Bert Altenburg and Alex Clarke, is now available in German.

The German translation was made by Berthold Leimbach and includes updates for the latest version of Xcode in Leopard.

This tutorial is for non-programmers, and is aimed at levelling the Cocoa learning curve as much as possible. In the best tradition, the tutorial is being released as a free booklet in pdf format.

Apart from the new German edition, Become an Xcoder is available in English, Japanese, Chinese and Arabic. It has been downloaded over 123,000 times since the first version was launched in March 2006. The book has found a new burst of popularity since the launch of the iPhone, with the influx of new programmers rushing to the platform, as it is an excellent introduction to Cocoa Touch programming as well.

It is available for download from Cocoalab. Direct Link for the PDF version: Become An Xcoder (German).


Read Full Post »

Just back from ESUG

What a blast to meet and discuss with so many people whose works I'm fan of! The five days conference put together over 170 participants, with a high concentration of languages designers and implementors.

A few notes of interest:

  • Newspeak, a new programming language designed by Gilad Bracha, is extremely promising. I haven’t seen such an important development in the field object-oriented languages (beside F-Script!) since Self in the early nineties. Trust me, you really want to check it out (there are a few papers and presentations floating around).
  • Gemstone System had a big presence at the conference. They are doing very cool stuff developing Gemstone/S, their beautiful object database. A big news for me is that Gemstone/S now runs on Mac OS X (I’m currently installing it from the DVD James Foster handed me). There is also a lot of work put into Maglev (i.e., not your daddy's Ruby VM).
  • Cog is a new open source virtual machine. Primarily developed for Croquet and Squeak, it will also be useable for other languages/environments. Cog is designed by Eliot Miranda, who is so experienced at this kind of stuff he can probably write a new VM while sleeping. Very promising technology.
  • Stephane Ducasse (with whom I wrote the OOPAL paper) is working on Pharo, which aims at producing a new Smalltalk environment. It consists in a radical redesign of Squeak and hopefully will produce a great platform in the coming years.
  • Andres Valloud's book on hashing is out. It's the only book I know of dedicated to the subject of analyzing and designing good hashing functions. It contains an extensive analysis of existing hash functions in various programming languages and offers many practical advices to design your owns. The examples are in Smalltalk but the principles and algorithms can be easily used in other languages.

Read Full Post »

It would be nice too have, don’t you think? Well, Doug Ransom just wrote to me about a bounty he is setting up for developing such an action. You can help growing the bounty or, if you are a bounty hunter, develop the Automator action.

Interested? Go to the bounty page to participate.

Read Full Post »

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

The following program illustrates subclassing Cocoa classes with F-Script 2.0. It displays a circle that can generate psychedelic effects by displaying flashes of colors. Here is a screenshot of the application:

The main component of the program is PsychedelicCircleView a subclass of NSView. This graphical component takes care of displaying the animated circle and handles mouse events, allowing the user to start and stop the psychedelic process. To that end, it implements the standards drawRect: and mouseDown: methods.

Below is the code for our NSView's subclass. To execute it, all you have to do is to copy/paste it in the F-Script console. Then copy/paste the code in charge of putting the component on screen (provided further below).

"Define the PsychedelicCircleView class, a subclass of Cocoa's NSView"
PsychedelicCircleView : NSView
  "Instance variables" 
  timer       "A NSTimer object used to animate the view" 
  message     "The message to display" 
  attributes  "A dictionary holding drawing attributes" 

  "Define the designated initializer"
  - initWithFrame:(NSRect)frame
     self := super initWithFrame:frame.
     self ~~ nil ifTrue:
         |font| "A local variable to hold the font object"

         "Determine the font to use.
         We use Synchro LET if available, the default user font otherwise"
         font := NSFont fontWithName:'Synchro LET' size:40.
         font == nil ifTrue:[ font := NSFont userFontOfSize:40 ].
         "Initialize the attributes dictionary used to draw the message"
         attributes := NSDictionary dictionaryWithObjects:{font} 

         "Initialize the message to display"
         message := '       Click to start/stop\nexpansion of consciousness'.
     ^ self

  "Define the method invoked by Cocoa to draw the view"
  - (void) drawRect:(NSRect)aRect
    "Define local variables"
    |red green blue size x y|  

    "Generate random values for color components"
    red   := 10 random / 9.
    green := 10 random / 9.
    blue  := 10 random / 9.
    "Set the color and draw the circle"
    (NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1) set.
    (NSBezierPath bezierPathWithOvalInRect:self bounds) fill.

    "If the psychedelic mode is not active, draw a message"  
    timer == nil ifTrue:
        "Compute coordinates of the message in order to have it centered"
        size := message sizeWithAttributes:attributes.
        x := self bounds extent x / 2 - (size width  / 2).
        y := self bounds extent y / 2 - (size height / 2).
        "Draw the message"
        message drawAtPoint:x<>y withAttributes:attributes.

  "Define the method invoked by Cocoa when the view is clicked"
  - (void) mouseDown:(NSEvent *)theEvent
    timer == nil ifTrue:
        "Create a NSTimer object to put the psychedelic process in motion"
        timer := NSTimer scheduledTimerWithTimeInterval:0.01 
                                                 target:[self setNeedsDisplay:YES] 
        "Stop the psychedelic process"
        timer invalidate.
        timer := nil.
        self setNeedsDisplay:YES.

The definition of our custom view class illustrates several elements of the class syntax: subclassing an existing Cocoa class, defining instance variables and methods, using self and super, etc. You’ll find further information in these posts:

Now that our view class is defined, we can put it in a window and play with it:

"Instantiate and configure a window"
window := NSWindow alloc initWithContentRect:(0<>0 extent:700<>700)
                                   styleMask:NSTitledWindowMask + NSClosableWindowMask + NSMiniaturizableWindowMask + NSResizableWindowMask

window setBackgroundColor:NSColor blackColor; 
       setTitle:'Psychedelic Circle'; 

"Instantiate and configure a psychedelic circle"
circle := PsychedelicCircleView alloc initWithFrame:window contentView bounds.
circle setAutoresizingMask:NSViewWidthSizable + NSViewHeightSizable.

"Put the circle view in the window"
window contentView addSubview:circle.

"Put the window onscreen"
window makeKeyAndOrderFront:nil.

The code above should open a window and display the psychedelic circle.

This is a good opportunity to experiment with live class redefinition. While the psychedelic circle is flashing, you can modify the program and see your modifications take effect immediately. For example, in the drawRect: method, replace the call to bezierPathWithOvalInRect: by a call to bezierPathWithRect:. Now, if you look at the flashing circle, you'll notice that it has turned into a flashing rectangle.

Note that, at the time of this writing, all the users of this program (i.e. me and my cat) are not experiencing an expansion of their consciousness by looking at the psychedelic circle. In fact, 50% of the users declare that the only thing this program expands is their headaches. The other 50% try compulsively to catch flies while producing strange sounds, just as usual.

Read Full Post »

When we define a method with F-Script (as shown here and here), it gets automatically registered in the Objective-C runtime. Indeed, from the point of view of the runtime, the new method is just like any other Objective-C method. Among other things, this means that it can be invoked from Objective-C code.

This also manifests in the syntax of F-Script itself. From the outside, nothing looks more like an Objective-C method than a F-Script method:

Objective-C method

- (float) doSomethingWithFoo:(int)x bar:(Bar *)y
  ... Objective-C code ...

F-Script method

- (float) doSomethingWithFoo:(int)x bar:(Bar *)y
  ... F-Script code ...

In the example above, we make use of explicit typing in the method signature. This is particularly useful when we want to hand out a F-Script object to some Objective-C code that call us back by invoking a method taking or returning non-object values. Indeed, the method we define must know what are the types of its arguments and return value.

Of course, since F-Script is a pure object language, actual values passed as arguments are automatically mapped to objects, and the returned object is automatically mapped to the data type promised to the caller by the signature.

Here is a list of types supported at the time of this writing:

  • id
  • Class
  • SEL
  • BOOL
  • _Bool
  • char
  • unsigned char
  • short
  • unsigned short
  • int
  • unsigned int
  • long
  • unsigned long
  • long long
  • unsigned long long
  • NSInteger
  • NSUInteger
  • float
  • double
  • CGFloat
  • NSRange
  • NSPoint
  • NSRect
  • NSSize
  • CGPoint
  • CGRect
  • CGSize
  • void *

We can also use a class name followed by a *, as in Objective-C (e.g. NSString *).
To specify pointers we can put as many * as needed after a type (e.g. unsigned int **).
Finally, we use void to indicate that a method returns nothing.

By default, in the absence of an explicit type, id is assumed, as in Objective-C.

To use these capabilities, get the latest alpha version of F-Script 2.0.

Read Full Post »