* Set (global) function argument from inside function
@ 2014-08-10 9:24 Thorsten Jolitz
2014-08-10 14:38 ` Stefan Monnier
0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Jolitz @ 2014-08-10 9:24 UTC (permalink / raw)
To: help-gnu-emacs
Hi List,
how can I set (global, not function-local) X from inside `foo' such that
I would see
,----
| #+results:
| : fooY: 7 Y: 7
`----
as the last result?
#+begin_src emacs-lisp
(defun foo (X)
(setq X (+ X 5)))
#+end_src
#+results:
: foo
#+begin_src emacs-lisp
(setq Y 2)
(message "fooY: %s Y: %s" (foo Y) Y)
#+end_src
#+results:
: fooY: 7 Y: 2
--
cheers,
Thorsten
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Set (global) function argument from inside function
2014-08-10 9:24 Set (global) function argument from inside function Thorsten Jolitz
@ 2014-08-10 14:38 ` Stefan Monnier
2014-08-11 7:53 ` Thorsten Jolitz
0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2014-08-10 14:38 UTC (permalink / raw)
To: help-gnu-emacs
> (defun foo (X)
> (setq X (+ X 5)))
>
> (setq Y 2)
> (message "fooY: %s Y: %s" (foo Y) Y)
> #+results:
> : fooY: 7 Y: 2
IIUC you want to pass a variable by reference. Elisp does not offer to
pass arguments by reference (contrary to, say, C++). But just like C,
it lets you construct references and pass them:
(defun foo (X-ref)
(setf (gv-deref X-ref) (+ (gv-deref X-ref) 5)))
(setq Y 2)
(message "fooY: %s Y: %s" (foo (gv-ref Y)) Y)
Note that gv-ref only exists in Emacs>24.3 and only works with
lexical-binding.
If you only care about global variables (rather than arbitrary "place"s,
such as global vars, local vars, array elements, list elements, ...),
and you want it to work with older Emacsen, you can use the following
instead:
(defun foo (X-name)
(set X-name (+ (symbol-value X-name) 5)))
(setq Y 2)
(message "fooY: %s Y: %s" (foo 'Y) Y)
-- Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Set (global) function argument from inside function
2014-08-10 14:38 ` Stefan Monnier
@ 2014-08-11 7:53 ` Thorsten Jolitz
0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Jolitz @ 2014-08-11 7:53 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> (defun foo (X)
>> (setq X (+ X 5)))
>>
>> (setq Y 2)
>> (message "fooY: %s Y: %s" (foo Y) Y)
>
>> #+results:
>> : fooY: 7 Y: 2
>
> IIUC you want to pass a variable by reference.
Exactly. Funny that I know the terminology in and out, but did not
relate it to the real-world problem.
> Elisp does not offer to pass arguments by reference (contrary to, say,
> C++). But just like C, it lets you construct references and pass
> them:
>
> (defun foo (X-ref)
> (setf (gv-deref X-ref) (+ (gv-deref X-ref) 5)))
>
> (setq Y 2)
> (message "fooY: %s Y: %s" (foo (gv-ref Y)) Y)
>
> Note that gv-ref only exists in Emacs>24.3 and only works with
> lexical-binding.
did not know about the gv.el library, and do not have the version with
`gv-deref' yet, but this definitely looks interesting.
> If you only care about global variables (rather than arbitrary "place"s,
> such as global vars, local vars, array elements, list elements, ...),
> and you want it to work with older Emacsen, you can use the following
> instead:
#+BEGIN_SRC emacs-lisp
(defun foo (X-name)
(set X-name (+ (symbol-value X-name) 5)))
#+END_SRC
#+results:
: foo
#+BEGIN_SRC emacs-lisp
(setq Y 2)
(message "fooY: %s Y: %s" (foo 'Y) Y)
#+END_SRC
#+results:
: fooY: 7 Y: 7
Exactly what I need, thank you!
--
cheers,
Thorsten
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-08-11 7:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-10 9:24 Set (global) function argument from inside function Thorsten Jolitz
2014-08-10 14:38 ` Stefan Monnier
2014-08-11 7:53 ` Thorsten Jolitz
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.