Notes on Hylang

This commit is contained in:
Correl Roush 2021-06-09 00:15:22 -04:00
parent fcc09a874d
commit c921b5a22a

59
20210608234049-hy.org Normal file
View file

@ -0,0 +1,59 @@
#+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
Supports [[file:20201007142751-python_typing.org][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