hy build-in with-decorator throws NameError: name 'with_decorator' is not defined

141 Views Asked by At

I'm trying to run the with-deocorator example from hy manual (https://docs.hylang.org/en/stable/language/api.html?highlight=with-decorator#with-decorator):

 (defn inc-decorator [func]
 (fn [value-1 value-2] (func (+ value-1 1) (+ value-2 1))))

 (with-decorator inc-decorator (defn addition [a b] (+ a b)))
 (setv foo (addition 1 1))
 (print foo)

but I'm getting an exception:

line 4, in <module>
(with-decorator inc-decorator (defn addition [a b] (+ a b)))
NameError: name 'with_decorator' is not defined

with_decorator with underscore. I'm trying with the latest stable version of hy. The same thing happens with any code that has with-decorator.

2

There are 2 best solutions below

1
On

I'm trying with the latest stable version of hy.

with-decorator was recently removed in master, whereas it still existed in 0.20.0, so I think you're mistaken. Check hy --version.

3
On

I found the documentation.

(with-decorator (app.route "/") (defn index [] "Hello World !"))

that worked in earlier versions is now done like this:

(defn [(.route app "/")] index [] "Hello World !")

and

(with-decorator (inc-decorator (defn addition [a b] (+ a b)))

is now

(defn [inc-decorator] addition [a b] (+ a b))