Syntactic objects



The scheme package allows the user to define syntactic objects. Syntactic objects are procedures with some differences: Syntactic objects can be used as argument in all procedure call that needs procedure type arguments. They can be used with apply, for-each, map, call/cc ...
lambda, define, let, if, cond, do, set! ... are built-in syntactic objects.

1 - Predefined functions.

(syntax object object ...) Creates a syntactic procedure. It has the same syntax as lambda
Returns the created syntax.
(syntax? object) Returns #t if  'object' is a syntax.  begin, define, let, if ... and objects created by method and syntax-method are syntactic objects. A syntax is also a procedure.
(syntax-method object object ...) Creates a syntactic method. It has the same syntax as method
Returns the created syntax.

2 - Examples.

The following example shows how to use syntax. We defines a 'while' syntax.
Note: This definition is tail-recursive (it runs in a constant stack space) since if, begin and apply are tail-recursive.

(define while
  (syntax (c . L)
    (if (local-eval c)
      (begin
        (apply begin L)
        (apply while c L)))))

Now we can use it to make iterations:

(let ((n 0))
  (while (< n 5)
         (display n)
         (set! n (+ n 1))))

The evaluation of the previous expressions produces the output :
01234
an returns an unspecified value.

The next example shows how to use syntax-method. This syntax is a method of the class <object> that gets a method of the class of the current object (see the object model of the scheme package for more details).

(<object> define get-method
  (syntax-method (name)
    (apply (<class> get get) (this get-class) name '())))

Now we can use it in the following way:

(define obj (<object> make))
(obj get-method get-class) ==> #[method get-class]


 Stéphane Hillion - 1998