Search code examples
hy

Use get in threading macro


The following three examples fail with the following errors:

(setv dct {1 2 3 4})
(setv dcts "dct")

;; (print (get (. (globals) [dcts]) 1))
(print (-> (globals) (. [dcts]) (get 1)))

;; (print (get (get (globals) "dct") 1))
(print (-> (globals) (get "dct") (get 1)))

;; (print (get (get (globals) dcts) 1))
(print (-> (globals) (get dcts) (get 1)))
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 267, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/home/shadowrylander/test.hy", line 5
    (print (-> (globals) (. [dcts]) (get 1)))
                                         ^
hy.errors.HySyntaxError: parse error for pattern macro 'get': got unexpected end of file, expected: some(...)
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 267, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/home/shadowrylander/test.hy", line 8
    (print (-> (globals) (get "dct") (get 1)))
                              ^
hy.errors.HySyntaxError: parse error for pattern macro 'get': got unexpected end of file, expected: some(...)
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 267, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "/home/shadowrylander/test.hy", line 11
    (print (-> (globals) (get dcts) (get 1)))
                              ^
hy.errors.HySyntaxError: parse error for pattern macro 'get': got unexpected end of file, expected: some(...)

Is it not possible to use get, or macros in general, with the threading macro (->)? I've rewritten the print statements multiple times to ensure consistent brackets, and they're all there.


Solution

  • => (require hyrule [->])
    
    (setv dct {1 2 3 4})
    (setv dcts "dct")
    
    ;; (print (get (. (globals) [dcts]) 1))
    (print (-> (globals) (. [dcts]) (get 1)))
    
    ;; (print (get (get (globals) "dct") 1))
    (print (-> (globals) (get "dct") (get 1)))
    
    ;; (print (get (get (globals) dcts) 1))
    (print (-> (globals) (get dcts) (get 1)))