unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Plist-put changes literal argument
@ 2020-09-02 23:55 Yuan Fu
  2020-09-02 23:59 ` Yuan Fu
  0 siblings, 1 reply; 7+ messages in thread
From: Yuan Fu @ 2020-09-02 23:55 UTC (permalink / raw)
  To: help-gnu-emacs

I found plist-put changes the literal argument I pass to a function:

(defun test ()
  (test-2 '(:a b)))

(defun test-2 (form)
  (print form)
  (plist-put form :a 'c)
  nil)

(progn (test) (test))

(:a b)

(:a c)
nil

I thought Elisp is pass-by-copy? Is this expected?

P.S., (setf (alist-get )) does the same, I guess this IS expected?

Yuan



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Plist-put changes literal argument
  2020-09-02 23:55 Plist-put changes literal argument Yuan Fu
@ 2020-09-02 23:59 ` Yuan Fu
  2020-09-03  0:18   ` Yuan Fu
  2020-09-05 15:53   ` Philipp Stephani
  0 siblings, 2 replies; 7+ messages in thread
From: Yuan Fu @ 2020-09-02 23:59 UTC (permalink / raw)
  To: help-gnu-emacs



> On Sep 2, 2020, at 7:55 PM, Yuan Fu <casouri@gmail.com> wrote:
> 
> I found plist-put changes the literal argument I pass to a function:
> 
> (defun test ()
>  (test-2 '(:a b)))
> 
> (defun test-2 (form)
>  (print form)
>  (plist-put form :a 'c)
>  nil)
> 
> (progn (test) (test))
> 
> (:a b)
> 
> (:a c)
> nil
> 
> I thought Elisp is pass-by-copy? Is this expected?
> 
> P.S., (setf (alist-get )) does the same, I guess this IS expected?
> 
> Yuan

I also tried setcar, same result. So Elisp doesn’t copy arguments?

Yuan


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Plist-put changes literal argument
  2020-09-02 23:59 ` Yuan Fu
@ 2020-09-03  0:18   ` Yuan Fu
  2020-09-03  0:53     ` Noam Postavsky
  2020-09-05 15:55     ` Philipp Stephani
  2020-09-05 15:53   ` Philipp Stephani
  1 sibling, 2 replies; 7+ messages in thread
From: Yuan Fu @ 2020-09-03  0:18 UTC (permalink / raw)
  To: help-gnu-emacs

> 
> I also tried setcar, same result. So Elisp doesn’t copy arguments?
> 
> Yuan


Ok, think more of it, it would be weird to make a full copy of cons (and other non-primitive values) on function call. I wonder how do other languages deal with this, because I never run into problems like this (except for in Python, with its default argument values).

Yuan


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Plist-put changes literal argument
  2020-09-03  0:18   ` Yuan Fu
@ 2020-09-03  0:53     ` Noam Postavsky
  2020-09-05 15:55     ` Philipp Stephani
  1 sibling, 0 replies; 7+ messages in thread
From: Noam Postavsky @ 2020-09-03  0:53 UTC (permalink / raw)
  To: Yuan Fu; +Cc: Help Gnu Emacs mailing list

On Wed, 2 Sep 2020 at 20:18, Yuan Fu <casouri@gmail.com> wrote:

> Ok, think more of it, it would be weird to make a full copy of cons
> (and other non-primitive values) on function call. I wonder how do
> other languages deal with this, because I never run into problems like
> this (except for in Python, with its default argument values).

In most languages you can't write a quoted list literal, so you would
end up writing code equivalent to

    (defun test ()
      (test-2 (list :a 'b)))



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Plist-put changes literal argument
  2020-09-02 23:59 ` Yuan Fu
  2020-09-03  0:18   ` Yuan Fu
@ 2020-09-05 15:53   ` Philipp Stephani
  1 sibling, 0 replies; 7+ messages in thread
From: Philipp Stephani @ 2020-09-05 15:53 UTC (permalink / raw)
  To: Yuan Fu; +Cc: help-gnu-emacs

Am Do., 3. Sept. 2020 um 01:59 Uhr schrieb Yuan Fu <casouri@gmail.com>:
>
>
>
> > On Sep 2, 2020, at 7:55 PM, Yuan Fu <casouri@gmail.com> wrote:
> >
> > I found plist-put changes the literal argument I pass to a function:
> >
> > (defun test ()
> >  (test-2 '(:a b)))
> >
> > (defun test-2 (form)
> >  (print form)
> >  (plist-put form :a 'c)
> >  nil)
> >
> > (progn (test) (test))
> >
> > (:a b)
> >
> > (:a c)
> > nil
> >
> > I thought Elisp is pass-by-copy? Is this expected?
> >
> > P.S., (setf (alist-get )) does the same, I guess this IS expected?
> >
> > Yuan
>
> I also tried setcar, same result. So Elisp doesn’t copy arguments?

Lists (actually all values except fixnums/small integers) are always
passed by reference; or, in other words, a list argument actually
contains a pointer to the list, not the list itself. Copying the
pointer doesn't copy the list.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Plist-put changes literal argument
  2020-09-03  0:18   ` Yuan Fu
  2020-09-03  0:53     ` Noam Postavsky
@ 2020-09-05 15:55     ` Philipp Stephani
  2020-09-06 15:23       ` Yuan Fu
  1 sibling, 1 reply; 7+ messages in thread
From: Philipp Stephani @ 2020-09-05 15:55 UTC (permalink / raw)
  To: Yuan Fu; +Cc: help-gnu-emacs

Am Do., 3. Sept. 2020 um 02:18 Uhr schrieb Yuan Fu <casouri@gmail.com>:
>
> >
> > I also tried setcar, same result. So Elisp doesn’t copy arguments?
> >
> > Yuan
>
>
> Ok, think more of it, it would be weird to make a full copy of cons (and other non-primitive values) on function call. I wonder how do other languages deal with this, because I never run into problems like this (except for in Python, with its default argument values).

Most other languages don't have literals of mutable types; or, if they
do, they create a new object for each evaluation of the literal
expression.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Plist-put changes literal argument
  2020-09-05 15:55     ` Philipp Stephani
@ 2020-09-06 15:23       ` Yuan Fu
  0 siblings, 0 replies; 7+ messages in thread
From: Yuan Fu @ 2020-09-06 15:23 UTC (permalink / raw)
  To: Philipp Stephani; +Cc: help-gnu-emacs



> On Sep 5, 2020, at 11:55 AM, Philipp Stephani <p.stephani2@gmail.com> wrote:
> 
> Am Do., 3. Sept. 2020 um 02:18 Uhr schrieb Yuan Fu <casouri@gmail.com <mailto:casouri@gmail.com>>:
>> 
>>> 
>>> I also tried setcar, same result. So Elisp doesn’t copy arguments?
>>> 
>>> Yuan
>> 
>> 
>> Ok, think more of it, it would be weird to make a full copy of cons (and other non-primitive values) on function call. I wonder how do other languages deal with this, because I never run into problems like this (except for in Python, with its default argument values).
> 
> Most other languages don't have literals of mutable types; or, if they
> do, they create a new object for each evaluation of the literal
> expression.

I see, that explains it. Thanks!

Yuan

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-09-06 15:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-02 23:55 Plist-put changes literal argument Yuan Fu
2020-09-02 23:59 ` Yuan Fu
2020-09-03  0:18   ` Yuan Fu
2020-09-03  0:53     ` Noam Postavsky
2020-09-05 15:55     ` Philipp Stephani
2020-09-06 15:23       ` Yuan Fu
2020-09-05 15:53   ` Philipp Stephani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).