# Block Specification Sheet

Portable Object Compiler (c) 1997,98,2000,03. 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:
- halt:value:
- ifError:
- value:ifError:
- on:do:
- value:on:do:

### Evaluating Blocks

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

### Control Flow

- repeatTimes:

## Methods

### errorHandler

> +errorHandler

Returns the current ifError: exception handler Block. If no ifError: message was sent, 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.

Note: Normally the ifError: message is used instead of errorHandler: to set an error handler.

### halt:value:

> +halt:message value:receiver

This message implements the halt: message of the Object class. It pops a handler of the stack of ifError: handlers and evaluates the handler block.

### 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:anObject ifError:aHandler

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

### on:do:

> -on:aClassOfExceptions do:aHandler

This message evaluates the receiver (like value) and provides a way to install aHandler Block as the exception handler for a certain class of exceptions. The aClassOfExceptions should be the name of the class, Exception or one of its subclasses. The handler Block takes one argument, the instance of the exception.

>     [ { ... } on:Exception do: { :anException | ... }];

See also: - signal (Exception)

### value:on:do:

> -value:anObject on:aClassOfExceptions do:aHandler

Like on:do: 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:firstObject value:secondObject

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

### intvalue:value:

> - (int)intvalue:firstObject value: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.