2021-07-29 22:51:04 +00:00
|
|
|
:PROPERTIES:
|
|
|
|
:ID: 295a5f40-5639-45e0-b277-8b5aecee600c
|
|
|
|
:END:
|
2021-06-09 04:15:22 +00:00
|
|
|
#+title: Hy
|
|
|
|
|
|
|
|
A lisp dialect of Python.
|
|
|
|
|
|
|
|
* Example
|
|
|
|
#+name: dataclass
|
|
|
|
#+begin_src hy :exports code :eval never
|
|
|
|
(import [dataclasses [dataclass])
|
|
|
|
|
|
|
|
(with-decorator dataclass
|
|
|
|
(defclass Thing []
|
|
|
|
(^str name)
|
|
|
|
(^int age)))
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
Disassembling the above code generates the following Python equivalent.
|
|
|
|
|
|
|
|
#+begin_src hy :exports results :cache yes :wrap src python
|
|
|
|
(disassemble
|
|
|
|
(quote
|
|
|
|
(do
|
|
|
|
(import [dataclasses [dataclass]])
|
|
|
|
|
|
|
|
(with-decorator dataclass
|
|
|
|
(defclass Thing []
|
|
|
|
(^str description)
|
|
|
|
(^int value)))))
|
|
|
|
True)
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
#+RESULTS[08e5153ad26dcf0fd8ca2a7c041eb26268461381]:
|
|
|
|
#+begin_src python
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Thing:
|
|
|
|
description: str
|
|
|
|
value: int
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
* Annotations
|
2021-07-29 22:51:04 +00:00
|
|
|
Supports [[id:5cb1f706-0162-4e6d-9cd8-dc6af3ae68cc][Python Typing]] annotations via the special form =^=:
|
2021-06-09 04:15:22 +00:00
|
|
|
|
|
|
|
#+begin_src hy :exports code :eval never
|
|
|
|
; Annotate the variable x as an int (equivalent to `x: int`).
|
|
|
|
(^int x)
|
|
|
|
; Can annotate with expressions if needed (equivalent to `y: f(x)`).
|
|
|
|
(^(f x) y)
|
|
|
|
|
|
|
|
; Annotations with an assignment: each annotation (int, str) covers the term that
|
|
|
|
; immediately follows.
|
|
|
|
; Equivalent to: x: int = 1; y = 2; z: str = 3
|
|
|
|
(setv ^int x 1 y 2 ^str z 3)
|
|
|
|
|
|
|
|
; Annotate a as an int, c as an int, and b as a str.
|
|
|
|
; Equivalent to: def func(a: int, b: str = None, c: int = 1): ...
|
|
|
|
(defn func [^int a ^str [b None] ^int [c 1]] ...)
|
|
|
|
#+end_src
|