About using macros described in OnLisp

I just don’t understand the description of the macro used for statements that create a context. It seems to me that if there is a binding, a macro is the only choice.

Is it impossible to achieve in other ways? What does the text below mean?

Many thanks.

There is another kind of context besides the lexical environment. in a broader sense, context is the state of the world, including the values ​​of special variables, the content of data structures, and the state of things outside of Lisp. Statements that create this type of context should be defined as macros, unless their code bodies are packed into closures. Context construction macro names often start with. The most commonly used macro of this type is probably with an open file. His body is evaluated by a newly opened file attached to a user variable:

(with-open-file (s "dump" :direction :output)
  (princ 99 s))

......

This statement must explicitly be defined as amacro because it binds s. However, operators that cause forms to be processed in a new context should still be defined as macros.

+4
1

, , :

  • ,
  • , ,

:

(this-is-some-function-with-some-file-opened (princ 99))

, princ this-is-some-function-with-some-file-opened. . .

, , . - .

:

(with-open-file (s "dump" :direction :output)
  (princ 99 s))

:

(call-with-open-file
   (lambda (s)
     (princ 99 s))
   "dump"
   :direction :output)

, . . Common Lisp . Lisp (OPEN, CLOSE, UNWIND-PROTECT) WITH-OPEN-FILE, , .

, , :

(call-with-open-file
   (lambda (s)
     (princ 99 s)
     ; 100 more lines here
     )
   "dump"
   :direction :output)

, , . , , Common Lisp - : , .

, . .

+6

All Articles