Edit
Attach
Printable
topic end
<!-- * Set TOPICTITLE = <nop>CoCreate Modeling code examples: Using symbol-function --> <style type="text/css"> pre { background-color:#FFEECC; } </style> #SymbolFunction ---++ <nop>CoCreate Modeling: Using <tt>symbol-function</tt> In the [[MacroTerminateEvent][termination event example]], we used an event to perform last rites while the application shuts down. Alternatively, it's possible to overload the Lisp =exit= function using the Common Lisp [[http://www.lispworks.com/documentation/HyperSpec/Body/f_symb_1.htm][<tt>symbol-function</tt> accessor]]. <pre> ;; -*-Lisp-*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Description: Hooks into exit to perform last rites ;; Author: Claus Brod ;; Created: 1/14/2005 ;; Language: Lisp ;; ;; (C) Copyright 2005 Claus Brod, all rights reserved ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; (in-package :clausbrod.de) (use-package :oli) (let ((original-exit (symbol-function 'exit))) (defun my-exit(&rest args) (when (equal :yes (oli:sd-display-question "Really, really quit?")) (if args (apply original-exit args) (funcall original-exit) ) ) ) (setf (symbol-function 'exit) (symbol-function 'my-exit)) ) </pre> Watch out: Your replacement for =exit= must fit the original function signature. Check your code carefully; when experimenting with this stuff, it's all too easy to end up with infinite recursions, creative crashes, and frustration. -- Main.ClausBrod - 14 Jan 2005 Claus, can you explain your rationale for doing the above inside a "let" form? Thanks, -- Main.TomBower - 11 Jan 2006 =let= establishes a lexical binding for =original-exit=; the variable is only valid within the scope of the =let= statement. This construct turns =original-exit= into something that _resembles_ a global variable, but without the drawbacks which are usually associated with global variables, such as namespace pollution or access to the variable from places which shouldn't know about it - with my approach, =original-exit= is only visible for the code which really needs to see it. To a C programmer, the closest analogy is a module-static variable: <pre> static function_pointer original_exit = symbol_function("exit"); bool my_exit(...) { ... } </pre> However, the C analogon is visible throughout the file where it is defined, whereas the Lisp variable =original-exit= (or rather its binding) is visible only to whatever is in the scope of =let=. The alternative would be to use a special variable at top-level, i.e. something like <pre> (defvar *original-exit* (symbol-function 'exit)) </pre> =defvar= makes =*original-exit*= a _special variable_ defined at top-level. I prefer the =let= approach and try to avoid special variables wherever I can. "Special variable" is in fact a technical term in Lisp and denotes a type of variables with _dynamic binding_, as opposed to _lexical binding_. This is confusing language lawyer stuff, but it is of practical relevance, and I hope I'll get around to discussing special variables and lexical binding soon. If you can't wait and need to know more now, read the chapter on "Variables" in Peter Seidel's wonderful [[http://www.gigamonkeys.com/book/variables.html][Practical Common Lisp]]. HTH. -- Main.ClausBrod - 11 Jan 2006 I elaborated on this now in my software blog - see [[http://www.clausbrod.de/Blog/BlogOnSoftware20060211]["I'm so special"]]. -- Main.ClausBrod - 12 Feb 2006 %COMMENT{type="below" nonotify="on"}%
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.11 |
>
|
r1.10
|
>
|
r1.9
|
Total page history
|
Backlinks
You are here:
CoCreateModeling
>
MacroSymbolFunction
r1.11 - 11 Feb 2020 - 18:17 -
ClausBrod
to top
CoCreateModeling
CoCreate Modeling
FAQ
Introduction
Hardware
Operating system
Memory management
File handling
Installation
Licensing
Graphics
App. knowhow
Lisp
Learning
Programming
Debugging
DDE
Compiler
Customization
Troubleshooting
Links
Code examples
Viewbench
News
Changes
Index
Search
Impressum
Home
Webs
Atari
Blog
Claus
CoCreateModeling
Klassentreffen
Main
Sandbox
Sommelier
TWiki
Xplm
My links
edit
TWiki
Welcome
TWiki Web TWiki Web Home Changes Topics Index Search
TWiki Webs Atari Blog Main
OneSpaceModeling
?
Sandbox TWiki TWiki Webs Atari Blog Main
OneSpaceModeling
?
Sandbox TWiki
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