Edit
Attach
Printable
topic end
<!-- * Set TOPICTITLE = Claus Brod: Fight globalization! (18 Feb 2006) --> <style type="text/css"> pre {background-color:#ffeecc;} </style> %STARTINCLUDE% <a name="18"></a> ---+++ [[BlogOnSoftware20060218][Fight globalization!]] (18 Feb 2006) <summary> [[BlogOnSoftware200602#11][A few days ago]], I talked everybody to sleep about special variables and dynamic bindings in Lisp. I somehow managed to avoid this topic for years, but then I finally had to understand it to fix subtle issues in our code when dealing with what I _thought_ were simple global variables. </summary> In Lisp, you usually declare a global variable using =defvar= and =defparameter= - but this way, the variable not only becomes global, but also _special_. They are probably called _special_ because of the special effects that they display - see my [[BlogOnSoftware200602#11][blog entry]] for an idea of the confusion this caused to a simple-minded C++ programmer (me). Most of the time, I would use =defvar= to emulate the effect of a "file-global" static variable in C++, and fortunately, this can be implemented in a much cleaner fashion using a =let= statement at the right spot. Example: <pre> // C++, file foobar.C static int globalFoo = 42; int foobar(void) { return globalFoo * globalFoo; } int foobar2(int newVal) { globalFoo = newVal; } ;; Lisp (let ((globalFoo 42)) (defun foobar1() (* globalFoo globalFoo)) (defun foobar2(newVal) (setf globalFoo newVal)) ) </pre> The =let= statement establishes a binding for =globalFoo= which is only accessible within =foobar1= and =foobar2=. This is even better than a static global variable in C++ at file level, because this way precisely the functions which actually have a business with =globalFoo= are able to use it; the functions =foobar1= and =foobar2= now share a variable. We don't have to declare a global variable anymore and thereby achieve better encapsulation _and_ at the same time avoid special variables with their amusing special effects. Life is good! This introduces another interesting concept in Lisp: Closures, i.e. functions with references to variables in their lexical context. More on this hopefully [[BlogOnSoftware20060223][soon]]. %COMMENT{type="below" nonotify="on"}% --- %STOPINCLUDE%
to top
End of topic
Skip to action links
|
Back to top
Edit
|
Attach image or document
|
Printable version
|
Raw text
|
Refresh
|
More topic actions
Revisions: | r1.8 |
>
|
r1.7
|
>
|
r1.6
|
Total page history
|
Backlinks
You are here:
Blog
>
BlogOnSoftware20060218
r1.8 - 05 Jan 2008 - 08:54 -
ClausBrod
to top
Blog
This site
2017
:
12
-
11
-
10
2016
:
10
-
7
-
3
2015
:
11
-
10
-
9
-
4
-
1
2014
:
5
2013
:
9
-
8
-
7
-
6
-
5
2012
:
2
-
10
2011
:
1
-
8
-
9
-
10
-
12
2010
:
11
-
10
-
9
-
4
2009
:
11
-
9
-
8
-
7
-
6
-
5
-
4
-
3
2008
:
5
-
4
-
3
-
1
2007:
12
-
8
-
7
-
6
-
5
-
4
-
3
-
1
2006:
4
-
3
-
2
-
1
2005:
12
-
6
-
5
-
4
2004:
12
-
11
-
10
C++
CoCreate Modeling
COM & .NET
Java
Mac
Lisp
OpenSource
Scripting
Windows
Stuff
Changes
Index
Search
Maintenance
Impressum
Datenschutzerklärung
Home
Webs
Atari
Blog
Claus
CoCreateModeling
Klassentreffen
Main
Sandbox
Sommelier
TWiki
Xplm
Jump:
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback