guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Functional record “setters”


From: Noah Lavine
Subject: Re: Functional record “setters”
Date: Tue, 10 Apr 2012 10:37:21 -0400

>>> However, generating the most optimal code may prove to be complicated.
>>> For instance, you’d want:
>>>
>>>   (set-fields p (person-address address-city) "Düsseldorf"
>>>                 (person-address address-street) "Bar")
>>>
>>> to expand to:
>>>
>>>   (set-person-address p
>>>                       (let ((a (person-address p)))
>>>                         (set-fields a (address-city) "Düsseldorf"
>>>                                       (address-street) "Bar")))
>>>
>>> But that would require knowledge of the relationship between
>>> ‘address-city’, ‘address-street’, and the underlying record type, etc.
>>
>> I don't understand why such knowledge is needed, or why this is
>> difficult.  We have procedural macros.  Simply sort the field-name-paths
>> lexicographically, split the sorted paths into groups with the same car,
>> and recurse.  Am I missing something?
>
> Yes: nothing forces you to prefix names with ‘address-’ here.

I might be wrong, but I don't think you need any knowledge of the
names to make this work. In your example, the two field-name-paths are

  (person-address address-city) and (person-address address-street).

They both have the car 'person-address, so you can pull that out to
generate your expansion:

   (set-person-address p
                       (let ((a (person-address p)))
                         (set-fields a (address-city) "Düsseldorf"
                                       (address-street) "Bar")))

I think it could be implemented without any knowledge of the names at
all. The lexicographical ordering is just a way to improve the
performance of finding common prefixes. A hash table would also work.

I hope this is useful,
Noah



reply via email to

[Prev in Thread] Current Thread [Next in Thread]