[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.15.2 Applicable objects

Gauche has a special hook to make an arbitrary object applicable.

Generic Function: object-apply object arg ...
If an object that is neither a procedure nor a generic function is applied to some arguments, the object and the arguments are passed to a generic function object-apply.

This can be explained better by examples.

For example, suppose you try to evaluate the following expression:

("abcde" 2)

The operator evaluates to a string, which is neither a procedure nor a generic fuction. So Gauche interpretes the expression as if it were like this:

(object-apply "abcde" 2)

Gauche doesn't define a method of object-apply that takes <string> and <integer> by default, so this signals an error. However, if you define such a method:

(define-method object-apply ((s <string>) (i <integer>))
  (string-ref s i))

Then the first expression works as if a string is applied on the integer:

("abcde" 2) => #\c

This mechanism works on almost all occasions where a procedure is allowed.

(apply "abcde" '(1))   => (#\b)
(map "abcde" '(3 2 1)) => (#\d #\c #\b)

Among Gauche built-in objects, <regexp> object and <regmatch> object have object-apply defined. See section 6.11 Regular expression.

Generic Function: (setter object-apply) object arg ... value
If a form of applying an applicable object appears in the first position of set! form, this method is called, that is:
(set! (object arg ...) value)
 => ((setter object-apply) object arg ... value)

This document was generated by Ken Dickey on November, 28 2002 using texi2html