hy

with-gensyms Error With Hy Documentation Example Code


I am fairly new to Hylang and I am trying to get the last two code snippets here to work. No matter what I do I get the error that with_gensyms is not defined. I am running hy version 0.27.0, which matches the version of the docs. Below is my sample code, followed by the error.

(import hy)
(import hyrule)

(defmacro nif [expr pos-form zero-form neg-form]
  (with-gensyms [g]
    `(do
       (setv ~g ~expr)
       (cond (> ~g 0) ~pos-form
             (= ~g 0) ~zero-form
             (< ~g 0) ~neg-form))))

(nif 0 (print "Positive") (print "Zero") (print "Negative"))
Traceback (most recent call last):
  File "<frozen runpy>", line 290, in run_path
hy.errors.HyMacroExpansionError: 
  File "/home/jwright/Downloads/macro.hy", line 22
    (nif 0 (print "Positive") (print "Zero") (print "Negative"))
    ^----------------------------------------------------------^
expanding macro nif
  NameError: name 'with_gensyms' is not defined

Solution

  • This chapter is still in need of major updates. In particular, with-gensyms was moved to Hyrule a few years ago, so you have to say (require hyrule [with-gensyms]) first.