I try to make a macro for defining ctypes.Structure. The macro is listed as follows:
(defmacro struct [name fields]
`(defclass ~name [ctypes.Structure]
[-fields-
~(lfor i (range 0 (len fields) 2)
(tuple [
(str (get fields (+ i 1)))
(get fields i)))]))
I think the lfor expression will generate a list of tuples, however, when I expand the macro using macroexpand, I found that the generated list is a list of lists, no tuple created. The result of macroexpand is given by:
=> (macroexpand '(struct Point [ctypes.c_int x ctypes.c_int y]))
HyExpression([
HySymbol('defclass'),
HySymbol('Point'),
HyList([
HySymbol('ctypes.Structure')]),
HyList([
HySymbol('-fields-'),
HyList([
HyList([
HyString('x'),
HySymbol('ctypes.c_int')]),
HyList([
HyString('y'),
HySymbol('ctypes.c_int')])])])])
I am quite confused that the tuple in the lfor expression seems to not work at all.
Since I am a hylang newbie, I am not familiar with the mechanism of macro. After several attempts, finally I make it work properly, as follows: