Skip to content

Instantly share code, notes, and snippets.

@edcrypt
Last active April 26, 2017 20:27
Show Gist options
  • Save edcrypt/934cda01dc23ad4b073c0cef3b33d936 to your computer and use it in GitHub Desktop.
Save edcrypt/934cda01dc23ad4b073c0cef3b33d936 to your computer and use it in GitHub Desktop.
Helper macro for attrs lib on Hy
; -*- mode: lisp; mode: paredit -*-
(defmacro defattrs [klass-name
&optional [super-klasses []] [options []] [attrs []]
&rest body]
`(do
(import attr)
(with-decorator ~(if options `(attr.s ~@options) 'attr.s)
(defclass ~klass-name ~super-klasses
~(list (interleave
(list-comp (if (coll? a) (first a) a) [a attrs])
(list-comp `(attr.ib ~@(if (coll? a)
(list (rest a)) []))
[a attrs])))
~@body))))
@edcrypt
Copy link
Author

edcrypt commented Apr 26, 2017

For even less boilerplate.
https://attrs.readthedocs.io/en/stable/
https://hylang.org

Similar to (defclass), but generates __init__(...) methods for you. Use it like this:

;; (defattrs ClassName [SuperClasses...] [options...] [attribs...])
(defattrs Coordinates [object] [:repr False]
  [x y [secret :init False]])

TODO attr.s(...) options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment