3 ms·
> Swift is the first language I've encountered that works this way; are there others? Lisp, of course :-) Common Lisp's SETF and related macros work this way.
by ScottBurson 11y ago
> Swift is the first language I've encountered that works this way; are there others?
Lisp, of course :-) Common Lisp's SETF and related macros work this way. E.g.,
(incf (ldb (byte 3 5) x))
would increment the "byte" (CL's term for an arbitrary bit field) of width 3 at offset 5 of X.
INCF is defined in terms of SETF. Of course, the set of macros that can update a place is user-extensible. So if you wanted one that multiplies the value in the place by two, you could just write something like this (the trailing "f" in the name, as you might guess, is the convention for such a macro):
(define-modify-macro doublef () (lambda (x) (* x 2)))
then you could call it like this:
(doublef (ldb (byte 3 5) x))
to double the value in the bit field.
(EDIT: used lambda expression instead of helper function.)