hy/tests/native_tests/with_decorator.hy
Gergely Nagy cbc2eed900
defclass reimagined
defclass now has a new syntax:

 (defclass Name [BaseList]
   [property value
    property value] ;; optional

   (defn method [self]
     self.property))

Anything after the optional property list (which will be translated to a
setv within the class context) will be added to the class body. This
allows one to have side effects and complex expressions within the class
definition.

As a side effect, defining methods is much more friendly now!

Closes #850.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
2015-08-04 16:43:07 +02:00

32 lines
609 B
Hy

(defn foodec [func]
(lambda [] (+ 1 1)))
(with-decorator foodec
(defn tfunction []
(* 2 2)))
(defn bardec [cls]
(setv cls.my_attr 123)
cls)
(with-decorator bardec
(defclass cls []
[my_attr 456]))
(defn test-decorator-clobbing []
"NATIVE: Tests whether nested decorators work"
(do
(defn dec1 [f] (defn k [] (+ (f) 1)))
(defn dec2 [f] (defn k [] (+ (f) 2)))
(with-decorator dec1
(with-decorator dec2
(defn f [] 1)))
(assert (= (f) 4))))
(defn test-decorators []
"NATIVE: test decorators."
(assert (= (tfunction) 2))
(assert (= cls.my_attr 123)))