Using if
inside a macro to generate different code
#2664
-
Hello again :) I am trying to write a macro that returns the model for a function, where inside the function something happens or not depending on a boolean parameter to the macro. In it's simplest form, here is what I have written: (defmacro m [v]
(if v
'(print "yes")
'(print "no")
)
)
(m False)
(m True) What I expect to happen is this code to compile to print("no")
print("yes") but what happens is that I get print("yes")
print("yes") This is especially surprising since if I read the code that is generated for the macro, the if statement looks correct to me: import hy
hy.macros.macro('m')(lambda v: hy.models.Expression([hy.models.Symbol('print', from_parser=True), hy.models.String('yes')]) if v else hy.models.Expression([hy.models.Symbol('print', from_parser=True), hy.models.String('no')])) I am obv. missing something trivially dumb here, but I've tried to reduce the example as much as possible, removing any quasiquotes/unquotes/splicing to be sure I'm not misunderstanding something and it looks like the else branch is just completely ignored... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Beta Was this translation helpful? Give feedback.
False
andTrue
come into the macro as symbols. The symbolFalse
is true. See http://hylang.org/hy/doc/v1.1.0/syntax#hy.models.Symbol