# Block Specification Sheet

Portable Object Compiler (c) 1997,98. All Rights Reserved.

# Block

Inherits from: Object

Maturity Index: Experimental

## Class Description

Objective-C Blocks are similar to block expressions in Smalltalk. Blocks are a way to express deferred computations; computations to be written at one place but invoked from another.

The Portable Object Compiler uses blocks for error handling i.e., the processing of exceptional conditions that interrupt the normal flow of program execution, such as out of memory, division by zero etc.

The Object method error: and the Block instance method ifError: are used for this.

Blocks are a non-standard part of the Objective-C language; this implementation, the first and most powerful as far as we know, is based on Brad Cox's TaskMaster paper where the same concept is discussed under the name of Action Expressions.

For a discussion on what Blocks are, and how to use them, we refer to the document Objective-C Blocks.

The compiler has an option -noBlocks that can be used to turn off the special syntax for Blocks.

## Method types

### Exception Handling

- errorHandler
- errorHandler:
- ifError:
- value:ifError:

### Evaluating Blocks

- value
- intvalue
- atExit
- value:
- intvalue:
- value:value:
- intvalue:value:

### Control Flow

- repeatTimes:

## Methods

### errorHandler

> +errorHandler

Returns the default handler, as set by errorHandler:, or, if none was explicitely set, it uses the following handler:

>     id errorHandler = { :msg :rcv | fprintf(stderr,[msg str]);abort(); };

### errorHandler:

> +errorHandler:aHandler

Make aHandler the default error handler. Returns the handler that was previously registered as default handler.

### ifError:

> -ifError:aHandler

Evaluates the receiver of the message and returns its return value. If an exception is raised, evaluates aHandler. This works by pushing aHandler on a stack of error handlers, the method halt: temporarily pops off an error handler and evaluates it with a message and receiver object as arguments :

>     [ { ... } ifError: { :msg :rcv | ... }];

Note: It is an error to have a non-local return from within the receiver, as this would leave an error handler dangling. It is allowed to return however, from within the error handler.

### value:ifError:

> -value:anObjectifError:aHandler

Like ifError: but the receiving block can take an argument.

### value

> -value

Evaluates the receiver of the message and returns its return value.

### intvalue

> - (int)intvalue

Evaluates the receiver of the message and returns its return value.

### atExit

> -atExit

Evaluates the receiver of the message when the process exits. See the ANSI C function atexit() for more details. There's a maximum of 32 exit Blocks that can be registered to be automatically called on exit. Blocks are evaluated in reverse order of their registration.

### value:

> -value:anObject

Evaluates, with anObject as argument, the receiver of the message and returns its return value.

### intvalue:

> - (int)intvalue:anObject

Evaluates, with anObject as argument, the receiver of the message and returns its return value.

### value:value:

> -value:firstObjectvalue:secondObject

Evaluates the receiver of the message with two arguments and returns its return value.

### intvalue:value:

> - (int)intvalue:firstObjectvalue:secondObject

Evaluates the receiver of the message with two arguments and returns its return value.

### repeatTimes:

> -repeatTimes:(int)n

Method to evaluate the receiver Block n times. Similar to the Smalltalk method timesRepeat: but with argument and receiver interchanged. Returns self.