Re: possible to write a with-object prc/macro ?

From: Lars Thomas Hansen <lth_at_ccs.neu.edu>
Date: Mon, 12 Jul 1999 18:37:28 -0400

Brian Denheyer <briand_at_deldotd.com> writes

>>>>>> "Lars" == Lars Thomas Hansen <lth_at_ccs.neu.edu> writes:
>
> Lars> An old Pascal hand, eh?
>
>No, just lazy ;-) But now I am intrigued. I'll have to go dig up a
>Pascal book and find out where this is used.

The Pascal/Modula family have a WITH statement that "opens up" an
instance of a record type. Given something like (pardon my syntax, it's
been a dozen years)

  RECORD Point
    x : INTEGER;
    y : INTEGER;
  END

you can do

  FUNCTION foo( self : Point ) : INTEGER;
  VAR z, x : INTEGER;
  BEGIN
    z := 22;
    x := 44;
    WITH self DO
      foo := x*y + z;
  END

In the body of the WITH, x and y are bound to "reference" variables that
point to the x and y fields inside the record self, if I remember
correctly.

Though the scope rules are clear -- WITH makes local bindings for all
the record fields, and the outer "x" is shadowed -- this gets real
interesting when you add fields to the record type later on, and the new
field suddenly shadows a local variable... consider adding a field "z"
to Point, above. Also, if your procedures are large, it can get a
little confusing what you actually mean by "x" without having to look
closely. For these reasons, WITH never seemed particularly attractive
to me, and I think the general consensus has gone in that direction too:
Modula-3 has a WITH statement that is really a LET, and Wirth co-opted
the WITH keyword for a different construct in Oberon.

And you're right, if you have get-slot/set-slot accessor/mutator
procedures, then R5RS macros should be able to do what I was suggesting.
For some reason I was stuck on your uses of x-of and y-of as generic
accessor procedures (a la Dylan).

--lars
Received on Tue Jul 13 1999 - 00:38:29 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST