From: ··········@gmail.com
Subject: reference
Date: 
Message-ID: <e871f230-1846-4f13-8762-b4b95e367378@q77g2000hsh.googlegroups.com>
hello all,

I am using Allegro 7.0 on XP.
I have a doubt regarding object assignment.

(setf new-obj (make-instance 'class-a))
(setf new-list (union new-list (list new-obj)))

after these two statements whether there will be two copies of newly
created instace? in other words new-list will have the whole object as
a copy or it will have only reference to the original object? how can
we make sure that new-list will have only reference?

thanks in advance
-Tushar

From: Pascal J. Bourguignon
Subject: Re: reference
Date: 
Message-ID: <7cr6fqrsg8.fsf@pbourguignon.anevia.com>
···········@gmail.com" <··········@gmail.com> writes:

> hello all,
>
> I am using Allegro 7.0 on XP.
> I have a doubt regarding object assignment.
>
> (setf new-obj (make-instance 'class-a))
> (setf new-list (union new-list (list new-obj)))
>
> after these two statements whether there will be two copies of newly
> created instace? in other words new-list will have the whole object as
> a copy or it will have only reference to the original object? 

You are rightly justified to have doubts, in the case of UNION.


SETF itself never copies anything really (you could consider that it
could copy fixnum and characters, in some way, but this is really an
orthogonal question).

So (setf new-obj (make-instance 'class-a)) just does what it looks
like it does, it creates an instance, and puts a reference to it in
the location of new-obj.

Try:

(let ((new-obj (make-instance 'class-a))
      other)
  (setf other new-obj)
  (length (remove-duplicates (list new-obj other) :test (function eql))))


In the case of UNION, it's unspecified whether UNION shall return a
copy of its arguments, when their union set-equals one of them.

(defun union (a b) ; ignoring other parameters for pedagogical clearness
   (if (and (subsetp a b) (subsetp b a))
      ; the sets a and b are equal, therefore their union is equal to both
      a
      (remove-duplicates (append a b)))) ; quick & dirty union.

is a conformant implementation (modulo missing parameters).

With it, (eql (union x x) x)
but:     (not (eql (union x nil) x))

But whatever the result of union, this result IS NOT copied, when
assigned to new-list, only a reference to the returned LIST (ie. (OR
CONS NULL)) is put in the location of new-list.


(let ((result (union old-list (list new-obj)))
      new-list)
  (setf new-list result)
  (length (remove-duplicates (list new-list result) :test (function eql))))

will still return 1 as above.



> how can
> we make sure that new-list will have only reference?

Don't you worry, it's the semantics of Lisp that make sure of that.

In lisp, there is no difference between a whole object and a reference
to that object.  References, locations, pointers, etc, are actually
implementation details that you can see only when you plunge deep in
the sources of your CL implementation.  What you have in lisp, are
objects, variables and bindings.


-- 
__Pascal Bourguignon__
From: Ken Tilton
Subject: Re: reference
Date: 
Message-ID: <47a9e93a$0$25030$607ed4bc@cv.net>
Pascal J. Bourguignon wrote:
> ···········@gmail.com" <··········@gmail.com> writes:
> 
> 
>>hello all,
>>
>>I am using Allegro 7.0 on XP.
>>I have a doubt regarding object assignment.
>>
>>(setf new-obj (make-instance 'class-a))
>>(setf new-list (union new-list (list new-obj)))
>>
>>after these two statements whether there will be two copies of newly
>>created instace? in other words new-list will have the whole object as
>>a copy or it will have only reference to the original object? 
> 
> 
> You are rightly justified to have doubts, in the case of UNION.

Hunh? Not about the newly created instance of class-a, about which the 
OP was asking. CLOS does not even have a copy capability.

kt

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Alex Mizrahi
Subject: Re: reference
Date: 
Message-ID: <47a9f4f0$0$90268$14726298@news.sunsite.dk>
 t> after these two statements whether there will be two copies of newly
 t> created instace?

can't you just run this code and see what happens?

 t>  in other words new-list will have the whole object as a copy or it will
 t> have only reference to the original object?

Common Lisp always operates on references.
there is no standard function to copy object

 t>  how can we make sure that new-list will have only reference?

you can be sure reading the documentation, trying yourself