tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.03k stars 48 forks source link

Add a `with-slots` macro #118

Open porky11 opened 8 years ago

porky11 commented 8 years ago

Something like this

(with-slots (a (b c)) x
  …)

should expand to this

(let ((a \ (: x a))
        (b \ (: x c)))
  …)

(like in CL)

porky11 commented 8 years ago

maybe it should not set the variables to pointers but the values, since setting multiple values of a struct is also possible using (setf struct ((a value1) (b value2) …))

tomhrr commented 8 years ago

Though this is useful, I'm not sure the core is the right place for it, at least at the moment. An example definition without error-checking has been added to the repository at https://github.com/tomhrr/dale/commit/8648e7670e6263bc46a7dabc372ec28323420b6e, though, if you want to use it in your own code.

porky11 commented 8 years ago

I wrote some version of this myself, but I'm not sure if it's useful, when you can't use it for both getting and setting values

tomhrr commented 8 years ago

The definition in the repository can be used to both get and set values, because the new bindings are pointers, rather than values. Is there something about it that doesn't work as expected?

porky11 commented 8 years ago

the problem if with-slots uses pointers, not values, it doesn't get that much easier: I'd still have to write (@ a), which is not much more than(@: x a), so I'm not sure, if it will be used this way. Maybe some macros would be more useful (some kind of read-macros may also be nice, but this should not be included in core. Dale as IR for higher level languages may be a good idea. The parser could be implemented as macro and allow some dialect of dale allowing read-macros or similar. It's easier to parse to dale code than llvm directly, so it would make a good IR I think, but this may be another issue)

porky11 commented 8 years ago

another more usable solution may be something like symbol-macrolet (which works similar to mfor), so that with-slots (and also with-accessors) may work as now, only that the variables will be symbol-macros, dereferencing the pointers, which are used in the current version.