roam/20210608234049-hy.org
2021-10-01 17:48:18 -04:00

62 lines
1.6 KiB
Org Mode

:PROPERTIES:
:ID: 295a5f40-5639-45e0-b277-8b5aecee600c
:END:
#+title: Hy
A lisp dialect of [[id:cda9c620-fec5-4549-b979-22fc06819d77][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 :eval no-export :cache yes :wrap src python
(disassemble
(quote
(do
(import [dataclasses [dataclass]])
(with-decorator dataclass
(defclass Thing []
(^str description)
(^int value)))))
True)
#+end_src
#+RESULTS[1efb8b1b457cd5e9ece84633e21daf77ea8e8753]:
#+begin_src python
from dataclasses import dataclass
@dataclass
class Thing:
description: str
value: int
#+end_src
* Annotations
Supports [[id:5cb1f706-0162-4e6d-9cd8-dc6af3ae68cc][Python Typing]] annotations via the special form =^=:
#+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