From: Surendra Singhi
Subject: How to declare an alias for a variable?
Date: 
Message-ID: <zmpegqnq.fsf@netscape.net>
Hello,
  How can I declare an alias for a variable? What I mean is that, for example
if I define a variable like below

(setq x 3)

then I want to be able to create an alias 'y' for 'x', such that if I change the
value of the value of 'x' then the value of 'y' should also change and vice
versa.  

Thanks for your help.

-- 

Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

From: justinhj
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <1129167787.002010.150930@g43g2000cwa.googlegroups.com>
Surendra Singhi wrote:
> Hello,
>   How can I declare an alias for a variable? What I mean is that, for example
> if I define a variable like below
>
> (setq x 3)
>
> then I want to be able to create an alias 'y' for 'x', such that if I change the
> value of the value of 'x' then the value of 'y' should also change and vice
> versa.
>
> Thanks for your help.
>
> --
>
> Surendra Singhi
> http://www.public.asu.edu/~sksinghi/index.html

Is this what you want ...

(setf x 3)
(setf y 'x)
y
> x
(symbol-value y)
> 3
(incf x)
(symbol-value y)
> 4
(incf (symbol-value y))
> 5

Justin
From: Surendra Singhi
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <u0fmgoeu.fsf@netscape.net>
"justinhj" <········@gmail.com> writes:

> Surendra Singhi wrote:
>> Hello,
>>   How can I declare an alias for a variable? What I mean is that, for example
>> if I define a variable like below
>>
>> (setq x 3)
>>
>> then I want to be able to create an alias 'y' for 'x', such that if I change the
>> value of the value of 'x' then the value of 'y' should also change and vice
>> versa.
>>
> Is this what you want ...
>
> (setf x 3)
> (setf y 'x)
> y
>> x
> (symbol-value y)
>> 3

I don't want to use symbol-value, I want the following behavior, 

CL_USER > y
3
CL_USER > (incf y)
4
CL_USER > x
4


Thanks.

-- 
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| Great wits are sure to madness near allied,
| And thin partitions do their bounds divide.
| 
|     (John Dryden, Absalom and Achitophel, 1681)
`----
From: Barry Margolin
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <barmar-275681.22121112102005@comcast.dca.giganews.com>
In article <············@netscape.net>,
 Surendra Singhi <·········@netscape.net> wrote:

> Hello,
>   How can I declare an alias for a variable? What I mean is that, for example
> if I define a variable like below
> 
> (setq x 3)
> 
> then I want to be able to create an alias 'y' for 'x', such that if I change 
> the
> value of the value of 'x' then the value of 'y' should also change and vice
> versa.  
> 
> Thanks for your help.

You can do it locally with SYMBOL-MACROLET.  There's no standard way to 
create a global alias.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Peter Seibel
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <m2irw211qd.fsf@gigamonkeys.com>
Barry Margolin <······@alum.mit.edu> writes:

> In article <············@netscape.net>,
>  Surendra Singhi <·········@netscape.net> wrote:
>
>> Hello,
>>   How can I declare an alias for a variable? What I mean is that, for example
>> if I define a variable like below
>> 
>> (setq x 3)
>> 
>> then I want to be able to create an alias 'y' for 'x', such that if I change 
>> the
>> value of the value of 'x' then the value of 'y' should also change and vice
>> versa.  
>> 
>> Thanks for your help.
>
> You can do it locally with SYMBOL-MACROLET.  There's no standard way to 
> create a global alias.

Sure there is: DEFINE-SYMBOL-MACRO. (Barry seems to have a mental
block on the fact that that particular feature made it into the final
version standard. ;-))

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Barry Margolin
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <barmar-926C05.00360114102005@comcast.dca.giganews.com>
In article <··············@gigamonkeys.com>,
 Peter Seibel <·····@gigamonkeys.com> wrote:

> Barry Margolin <······@alum.mit.edu> writes:
> 
> > In article <············@netscape.net>,
> >  Surendra Singhi <·········@netscape.net> wrote:
> >
> >> Hello,
> >>   How can I declare an alias for a variable? What I mean is that, for 
> >>   example
> >> if I define a variable like below
> >> 
> >> (setq x 3)
> >> 
> >> then I want to be able to create an alias 'y' for 'x', such that if I 
> >> change 
> >> the
> >> value of the value of 'x' then the value of 'y' should also change and 
> >> vice
> >> versa.  
> >> 
> >> Thanks for your help.
> >
> > You can do it locally with SYMBOL-MACROLET.  There's no standard way to 
> > create a global alias.
> 
> Sure there is: DEFINE-SYMBOL-MACRO. (Barry seems to have a mental
> block on the fact that that particular feature made it into the final
> version standard. ;-))

I couldn't remember whether it did or didn't, and tried to find it in 
the hyperspec.  There's no link to its dictionary entry from the 
glossary entry for "symbol macro".

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Duane Rettig
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <4psq8353k.fsf@franz.com>
Barry Margolin <······@alum.mit.edu> writes:

> In article <··············@gigamonkeys.com>,
>  Peter Seibel <·····@gigamonkeys.com> wrote:
>
>> Sure there is: DEFINE-SYMBOL-MACRO. (Barry seems to have a mental
>> block on the fact that that particular feature made it into the final
>> version standard. ;-))
>
> I couldn't remember whether it did or didn't, and tried to find it in 
> the hyperspec.  There's no link to its dictionary entry from the 
> glossary entry for "symbol macro".

When in doubt, use a good search tool:

http://www.franz.com/search/index.lhtml#ansispec

Try entering symbol-macro into the ANSI Spec search box there.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Wade Humeniuk
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <Ljk3f.19735$Io.7479@clgrps13>
Surendra Singhi wrote:
> Hello,
>   How can I declare an alias for a variable? What I mean is that, for example
> if I define a variable like below
> 
> (setq x 3)
> 
> then I want to be able to create an alias 'y' for 'x', such that if I change the
> value of the value of 'x' then the value of 'y' should also change and vice
> versa.  
> 
> Thanks for your help.
> 

CL-USER 1 > (defvar x 3)
X

CL-USER 2 > (define-symbol-macro y x)
X

CL-USER 3 > y
3

CL-USER 4 > x
3

CL-USER 5 > (incf x)
4

CL-USER 6 > y
4

CL-USER 7 >

Wade
From: Surendra Singhi
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <1x2qgiyj.fsf@netscape.net>
Wade Humeniuk <··················@telus.net> writes:

> Surendra Singhi wrote:
>> Hello,
>>   How can I declare an alias for a variable? What I mean is that, for example
>> if I define a variable like below
>> (setq x 3)
>> then I want to be able to create an alias 'y' for 'x', such that if
>> I change the
>> value of the value of 'x' then the value of 'y' should also change and vice
>> versa. 
>
> CL-USER 1 > (defvar x 3)
> X
>
> CL-USER 2 > (define-symbol-macro y x)
> X
> [..code snipped..]

Wade and Barry thanks for your help.

Is what I am doing below correct? Also, I am stuck at getting one desired
behavior. 

(defvar x 3)

(defclass object()
  ((data :initarg :data)))

(define-symbol-macro y
    (make-instance 'object :data x))

(slot-value y 'data)

OUTPUT> 3

(setq x 20)

OUTPUT> 20

(slot-value y 'data)

OUTPUT> 20

Fine.


(setf (slot-value y 'data) 100)

100

OUTPUT> x

20

OUTPUT> (slot-value y 'data)
20

Why? Can I set 'x' from 'y' now, if not, then how should I define x?


Thanks once again.

-- 
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| Great wits are sure to madness near allied,
| And thin partitions do their bounds divide.
| 
|     (John Dryden, Absalom and Achitophel, 1681)
`----
From: Peter Seibel
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <m2ek6q11cl.fsf@gigamonkeys.com>
Surendra Singhi <·········@netscape.net> writes:

> Wade Humeniuk <··················@telus.net> writes:
>
>> Surendra Singhi wrote:
>>> Hello,
>>>   How can I declare an alias for a variable? What I mean is that, for example
>>> if I define a variable like below
>>> (setq x 3)
>>> then I want to be able to create an alias 'y' for 'x', such that if
>>> I change the
>>> value of the value of 'x' then the value of 'y' should also change and vice
>>> versa. 
>>
>> CL-USER 1 > (defvar x 3)
>> X
>>
>> CL-USER 2 > (define-symbol-macro y x)
>> X
>> [..code snipped..]
>
> Wade and Barry thanks for your help.
>
> Is what I am doing below correct? Also, I am stuck at getting one
> desired behavior.
>
> (defvar x 3)
>
> (defclass object()
>   ((data :initarg :data)))
>
> (define-symbol-macro y
>     (make-instance 'object :data x))
>
> (slot-value y 'data)
>
> OUTPUT> 3
>
> (setq x 20)
>
> OUTPUT> 20
>
> (slot-value y 'data)
>
> OUTPUT> 20
>
> Fine.
>
>
> (setf (slot-value y 'data) 100)
>
> 100
>
> OUTPUT> x
>
> 20
>
> OUTPUT> (slot-value y 'data)
> 20
>
> Why? Can I set 'x' from 'y' now, if not, then how should I define x?

Leaving aside the questions I *ought* to be asking about *why* you
want to do this, it seems like you want this:

  CL-USER> (defvar *y* (make-instance 'object :data 3))
  *Y*
  CL-USER> (define-symbol-macro *x* (slot-value *y* 'data))
  *X*
  CL-USER> (setf *x* 30)
  30
  CL-USER> (slot-value *y* 'data)
  30
  CL-USER> (setf (slot-value *y* 'data) 300)
  300
  CL-USER> *x*
  300

But this is probably not what you really want to be doing unless
you're trying to obfuscate your code.

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Surendra Singhi
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <r7apucvo.fsf@netscape.net>
Hello Peter,

Peter Seibel <·····@gigamonkeys.com> writes:
>>> Surendra Singhi wrote:
>> (defvar x 3)
>>
>> (defclass object()
>>   ((data :initarg :data)))
>>
>> (define-symbol-macro y
>>     (make-instance 'object :data x))
>>
>> (slot-value y 'data)
>>
>> OUTPUT> 3
>>
>> (setq x 20)
>>
>> OUTPUT> 20
>>
>> (slot-value y 'data)
>>
>> OUTPUT> 20
>>
>> Fine.
>>
>>
>> (setf (slot-value y 'data) 100)
>>
>> 100
>>
>> OUTPUT> x
>>
>> 20
>>
>> OUTPUT> (slot-value y 'data)
>> 20
>>
>> Why? Can I set 'x' from 'y' now, if not, then how should I define x?
>
> Leaving aside the questions I *ought* to be asking about *why* you
> want to do this, it seems like you want this:

The reason why I want to do this is because I have an ffi variable, (to be
concrete say 'x') which I want to make available as a value of the slot 'data'
in an instance of the class 'object'. This instance 'y' of class 'object'
should be created at load time. 

The problem is that the ffi variable 'x' has the value nil when the
library is loaded, but when the program is executed, it points to some 'C'
object. So, I need to be able to assign a value to the slot 'data' such  that
it changes whenever the value of 'x' changes.   

Does this sound reasonable?

>   CL-USER> (defvar *y* (make-instance 'object :data 3))
>   *Y*
>   CL-USER> (define-symbol-macro *x* (slot-value *y* 'data))
>   *X*
>   CL-USER> (setf *x* 30)
>   30
>   CL-USER> (slot-value *y* 'data)
>   30
>   CL-USER> (setf (slot-value *y* 'data) 300)
>   300
>   CL-USER> *x*
>   300

Thanks for it, but what I want is slightly different, I want an already
defined variable name to be equivalent to a slot in an instance, rather than
defining a new variable name. 

Thanks once again.
-- 
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| Great wits are sure to madness near allied,
| And thin partitions do their bounds divide.
| 
|     (John Dryden, Absalom and Achitophel, 1681)
`----
From: Wade Humeniuk
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <Mpr3f.20837$yS6.17603@clgrps12>
Surendra Singhi wrote:

> 
> Thanks for it, but what I want is slightly different, I want an already
> defined variable name to be equivalent to a slot in an instance, rather than
> defining a new variable name. 
> 

It would be simpler to create an accessor method that is the "alias" of the
variable x.  Something like..  (here x <==> (data y))

CL-USER 1 > (defvar x nil)
X

CL-USER 2 > (defclass object () ())
#<STANDARD-CLASS OBJECT 20682D9C>

CL-USER 3 > (defmethod data ((obj object)) x)
#<STANDARD-METHOD DATA NIL (OBJECT) 2067BC94>

CL-USER 4 > (defmethod (setf data) (new-value (obj object))
               (setf x new-value))
#<STANDARD-METHOD (SETF DATA) NIL (T OBJECT) 20675144>

CL-USER 5 > (setf y (make-instance 'object))
#<OBJECT 20693F44>

CL-USER 6 > (data y)
NIL

CL-USER 7 > (setf (data y) 10)
10

CL-USER 8 > x
10

CL-USER 9 > (setf x 128)
128

CL-USER 10 > (data y)
128

CL-USER 11 >

Wade
From: Surendra Singhi
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <4q7k295i.fsf@netscape.net>
Wade Humeniuk <··················@telus.net> writes:

> Surendra Singhi wrote:
>
>> Thanks for it, but what I want is slightly different, I want an
>> already
>> defined variable name to be equivalent to a slot in an instance, rather than
>> defining a new variable name.
>
> It would be simpler to create an accessor method that is the "alias" of the
> variable x.  Something like..  (here x <==> (data y))
>

Thanks Kalle and Wade.

-- 
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| "All animals are equal, but some animals are more equal than others."
|     -- Orwell, Animal Farm, 1945
`----
From: Kalle Olavi Niemitalo
Subject: Re: How to declare an alias for a variable?
Date: 
Message-ID: <873bn5gbv1.fsf@Astalo.kon.iki.fi>
Surendra Singhi <·········@netscape.net> writes:

> (define-symbol-macro y
>     (make-instance 'object :data x))

When you evaluate the symbol Y after this, it expands to
(make-instance 'object :data x), which constructs a fresh
instance and stores the current value of X to its DATA slot.

> (setf (slot-value y 'data) 100)

This constructs an instance as above, and then changes its DATA
slot to 100.  The value of X is unaffected.  The modified
instance is lost and will presumably be garbage collected.

> (slot-value y 'data)

This constructs another instance, initializing its DATA slot with
the value of X, and evaluates to that value.  It does not matter that
the DATA slot of another instance was previously changed to 100.

Peter Seibel already described how you can bind *Y* to an
instance and make *X* expand to a form that accesses its
DATA slot (although I disagree with his putting asterisks
in the symbol macro *X*, as LET will not bind it dynamically
by default).  There is also another way: to keep the actual
value in *X* and make SLOT-VALUE forward to it.  In standard
Common Lisp, this can be done with the following ugly code:

(defvar *x* 3)

(defclass object ()
  ;; Do not put a DATA slot here.  Just pretend there is one.
  ())

(defmethod slot-missing ((class standard-class) (object object)
                         (slot-name (eql 'data)) operation
                         &optional new-value)
  ;; If the operation is not one of the four allowed by the spec,
  ;; I'll rather get an error about that than call the next method.
  (ecase operation
    (setf (setq *x* new-value))
    (slot-boundp (boundp '*x*))
    (slot-makunbound (makunbound '*x*))
    (slot-value (if (boundp '*x*) *x*
                  ;; Signal an unbound-slot error, rather than
                  ;; unbound-variable.  Slot-unbound is "not intended
                  ;; to be called by programmers" but I don't see what
                  ;; else to do here.
                  (slot-unbound class object slot-name)))))

(defparameter *y* (make-instance 'object))

The MOP described in AMOP would allow this to be written in a
cleaner way, so that the system knows that the slot exists.