Macro to detect global scope -



Macro to detect global scope -

from understand, mutual lisp has no global lexical scope, if want create global variable have utilize defvar instead of setq. programmer, i'm indignant having remember this. in believe in spirit of lisp, i'm trying create macro automatically. i'd write

(= x 1)

and have "just work" no matter am. imagine macro like

(defmacro = (name value) `(,(if (is-global) 'defvar 'setf) ,name ,value))

how can write is-global?

introducing bindings

from understand, mutual lisp has no global lexical scope,

this part correct, there "typical" workarounds. there's no canonical implementation, searching deflexical can lead implementations.

so if want to create a global variable have utilize defvar instead of setq. (emphasis added)

this incorrect. part, don't create variables. introduce bindings environment. mutual way of doing let or arguments function. e.g.:

(defun foo (bar) ;; in here, there's variable `bar` ) (let ((a ...)) ;; bound in here

these lexical bindings, unless symbol identifies variable in source declared special, in mutual lisp means it's dynamically scoped variable. can special declarations like:

(defun foo (bar) (declare (special bar)) ;; in here, there's variable `bar` ) (let ((a ...)) (declare (special a)) ;; bound in here

now, can introduce global variables defparameter , defvar. these globally declare variable special.

updating bindings

in both cases, can utilize setq or setf update value of variable. is, can utilize setq or setf update value of lexical variables special variables. can do:

(defparameter *cat* (make-initial-cat)) (let ((cat (some-local-cat))) (setf *cat* (make-instance 'cat)) ; update global/dynamic (setf cat (make-instance 'cat)) ; update local/lexical

setf works in both cases, sounds assignment operator you're looking setf.

the issue sounds you're trying work around you're not supposed utilize setf/setq undeclared variables. indeed, that's undefined behavior. sounds you're trying create assignment operator automatically introduce variable if there's not 1 in surrounding environment. can't this, @ to the lowest degree 2 reasons:

how know whether introduce lexical or dynamic variable? can't determine surrounding environment, because if in surrounding environment, wouldn't need introduce it. there's no way check whether it's declared local or dynamic variable. there workaround alternatives work in cases, environment access isn't part of standard mutual lisp. (see related question: macro observe global scope. implementations implement cltl2 environnments api.)

macros scope lisp common-lisp

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -