1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

add define-once

* module/ice-9/boot-9.scm (define-once): New syntax.

* doc/ref/api-binding.texi (Top Level):
* NEWS: Add notes about define-once.
This commit is contained in:
Andy Wingo 2011-02-09 20:54:22 +01:00
parent 9970cf6708
commit c5f30c4cba
3 changed files with 28 additions and 1 deletions

5
NEWS
View file

@ -10,6 +10,11 @@ latest prerelease, and a full NEWS corresponding to 1.8 -> 2.0.
Changes since the 1.9.15 prerelease: Changes since the 1.9.15 prerelease:
** New syntax: define-once
`define-once' is like Lisp's `defvar': it creates a toplevel binding,
but only if one does not exist already.
** Improved exactness handling for complex number parsing ** Improved exactness handling for complex number parsing
When parsing non-real complex numbers, exactness specifiers are now When parsing non-real complex numbers, exactness specifiers are now

View file

@ -1,6 +1,6 @@
@c -*-texinfo-*- @c -*-texinfo-*-
@c This is part of the GNU Guile Reference Manual. @c This is part of the GNU Guile Reference Manual.
@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010, 2011
@c Free Software Foundation, Inc. @c Free Software Foundation, Inc.
@c See the file guile.texi for copying conditions. @c See the file guile.texi for copying conditions.
@ -88,6 +88,23 @@ longer visible.
Attention: Scheme definitions inside local binding constructs Attention: Scheme definitions inside local binding constructs
(@pxref{Local Bindings}) act differently (@pxref{Internal Definitions}). (@pxref{Local Bindings}) act differently (@pxref{Internal Definitions}).
Many people end up in a development style of adding and changing
definitions at runtime, building out their program without restarting
it. (You can do this using @code{reload-module}, the @code{reload} REPL
command, the @code{load} procedure, or even just pasting code into a
REPL.) If you are one of these people, you will find that sometimes you
there are some variables that you @emph{don't} want to redefine all the
time. For these, use @code{define-once}.
@fnindex defvar
@deffn {Scheme Syntax} define-once name value
Create a top level variable named @var{name} with value @var{value}, but
only if @var{name} is not already bound in the current module.
@end deffn
Old Lispers probably know @code{define-once} under its Lisp name,
@code{defvar}.
@node Local Bindings @node Local Bindings
@subsection Local Variable Bindings @subsection Local Variable Bindings

View file

@ -473,6 +473,11 @@ If there is no handler at all, Guile prints an error and then exits."
(with-syntax ((s (datum->syntax x (syntax-source x)))) (with-syntax ((s (datum->syntax x (syntax-source x))))
#''s))))) #''s)))))
(define-syntax define-once
(syntax-rules ()
((_ sym val)
(define sym
(if (module-locally-bound? (current-module) 'sym) sym val)))))