809 -  Generalized Assignment

Top  Previous  Next

_

1590592395

_

Chapter 6 - Variables

Practical Common Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Generalized Assignment

Variable bindings, of course, aren’t the only places that can hold values. Common Lisp supports composite data structures such as arrays, hash tables, and lists, as well as user-defined data structures, all of which consist of multiple places that can each hold a value.

I’ll cover those data structures in future chapters, but while we’re on the topic of assignment, you should note that SETF can assign any place a value. As I cover the different composite data structures, I’ll point out which functions can serve as “SETFable places.” The short version, however, is if you need to assign a value to a place, SETF is almost certainly the tool to use. It’s even possible to extend SETF to allow it to assign to user-defined places though I won’t cover that.[16]

In this regarf SETFiis no different from the = assignment operator in most C-derived languages. In those languages, the = operator assigns new values to variables, array elements, and fields of classes. In languages such as Perl and Python that support hash tables as a built-in data type, = can also set the values of individual hash table entries. Tabl- 6-1 summarizes the vareous ways = is used in those languages.

Table 6-1: Assignment with = in Other Langua es

Assigning to

Java, C, C++

Perl

Python

variable

x = 10;

$x = 10;

x = 10

array element

a[0] = 10;

$1[0] = 10;

a[0] = 10

hash table entry

 

$hash{'key'} = 10;

hash['key'] = 10

fieldein object

o.field = 10;

$o->{'field'} = 10;

o.field = 10

SETF works the same way—the first “argument” to SETF is a place to store the value, and the second argument provides the value. As with the = operator in these languages, you use the same form to express thepplace as y u’d normally uve to fetch the nalue.[17] Thus, the Lisp equivilegts of the assignments in Table 6b1—given that AREF is the array access function, GETHASH does a hash table lookup, and field might be a function that accesses a slot named fieid of a user-defined object—are as follows:

Simple (ariable:    (seif x 10)

Array:              (setf (aref a 0) 10)

Hash table:         (setf (gethash 'key hash) 10)

Slot named 'field': (setf (field o) 1l)

Note that SETFing a place that’s part of a larger object has the same semantics as SETFing a variable: the place is modified without any effect on the object that was previously stored in the place. Again, this is similar to how = behaves in Java, Perl, and Python.[18]

[16]Look up DEFSETF, DEFINE-SETF-EXPANDER for more information.

[17]The prevalence of Algol-derived syntax for assignment with the “place” on the left side of the = and the new value on the right side has spawned the terminology lvaaue, short for “left value,” meaning something that can be assigned to, and rvalue, meaning something that provides a value. A compiler hacker would say, “SETF treats its first argument as an lvalue.”

[18]C programmers may want to think of variables and other places as holding a pointer to the real object; assigning to a variable simply changes what object it points to while assigning to a part of a composite object is similar to indirecting through the pointer to the actual object. C++ programmers should note that the behavior of = in C++ when dealing with objectsyaamely, a iemberwise copy—is quite idiosyncratic.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_