chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] define not using define


From: Elf
Subject: Re: [Chicken-users] define not using define
Date: Fri, 25 Jan 2008 08:43:20 -0800 (PST)

interesting you bring this up.  ive been working on a new set of condition
macro extensions for handling this sort of behaviour. :)

-elf


On Fri, 25 Jan 2008, Graham Fawcett wrote:

On Jan 25, 2008 10:32 AM, Elf <address@hidden> wrote:

on that note, something that does work in the general case:

(define-macro (define-if-undefined name . val)
     `(condition-case ,name
         (()    (define ,name ,@val))))

Thanks, Elf, I missed the "unless already exists" requirement.
(define-if-undefined) is reminiscent of DEFVAR in Common Lisp.

On a tangent, this reminds me of a macro I use. I wanted to introduce
formal hook-points in the main program, and let the run-script inject
hook-procedures into the main code if desired. The macro

(define-macro (hook-point hook-name)
 (let ((hook (gensym "hook")))
   `(and-let* ((,hook (condition-case ,hook-name (() #f))))
      (,hook))))

is used in the main program like this:

(use ...)
(hook-point pre-init)
(initialize)
(hook-point post-init)
(main-loop)
(hook-point pre-shutdown)
(shutdown)

if the runscript defines a (pre-init) thunk, it's called at the right
moment; if not, no error is thrown, and the program continues. And I
like the way the main program reads; it's clear where the optional
behaviour might occur.

Graham





reply via email to

[Prev in Thread] Current Thread [Next in Thread]