roam/20210608234049-hy.org

63 lines
1.6 KiB
Org Mode
Raw Normal View History

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
2021-08-06 17:21:42 +00:00
A lisp dialect of [[id:cda9c620-fec5-4549-b979-22fc06819d77][Python]].
2021-06-09 04:15:22 +00:00
* 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.
2022-07-17 01:40:28 +00:00
#+begin_src hy :exports results :eval never :cache yes :wrap src python
2021-06-09 04:15:22 +00:00
(disassemble
(quote
(do
(import [dataclasses [dataclass]])
(with-decorator dataclass
(defclass Thing []
(^str description)
(^int value)))))
True)
#+end_src
2021-10-01 21:48:18 +00:00
#+RESULTS[1efb8b1b457cd5e9ece84633e21daf77ea8e8753]:
2021-06-09 04:15:22 +00:00
#+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