hy icon indicating copy to clipboard operation
hy copied to clipboard

Help understanding hy.macros.-hy-macros initialization.

Open ekaschalk opened this issue 7 years ago • 5 comments

First run will print hy.macros.-hy-macros fine. Second and subsequent runs will print an empty dict.

Deleting the foo.pyc will reset the -hy-macros dict as expected.

Is this intended behavior? If intended - how do I test code referencing -hy-macros?

pkg/foo.hy

(import hy.macros)

(defn foo []
  (print hy.macros.-hy-macros))

pkg/test_foo.hy

(import [pkg.foo [foo]])

(defn test-foo []
  (foo)
  (doc ->))  ; Similarly, will print on first runthrough, error on second run

ekaschalk avatar Dec 16 '17 21:12 ekaschalk

Maybe related to #1324.

It's easy to force a compile at runtime though, just use hy.eval, e.g.

(hy.eval '(print hy.macros._hy-macros))

gilch avatar Dec 16 '17 23:12 gilch

Ok thanks a simple (hy.eval '(import hy.macros)) in the module being tested does the trick.

ekaschalk avatar Dec 16 '17 23:12 ekaschalk

I don't think hy.macros._hy_macros is intended for public use.

Kodiologist avatar Dec 17 '17 15:12 Kodiologist

Is there a better way to access all defined macros? doc takes the same approach (and has the same error on a second run). It's for code introspection for Emacs, not sure if that qualifies as public use.

ekaschalk avatar Dec 17 '17 16:12 ekaschalk

No, I don't think so. We've never worked on introspection of the macro system beyond Hy's own internal needs.

Kodiologist avatar Dec 17 '17 16:12 Kodiologist

Better support for introspection of the macro system could replace hy.reserved and perhaps also delmacro.

Kodiologist avatar Feb 25 '23 19:02 Kodiologist