roam/20210608234049-hy.org
2021-07-29 18:51:25 -04:00

1.5 KiB

Hy

A lisp dialect of Python.

Example

  (import [dataclasses [dataclass])

  (with-decorator dataclass
    (defclass Thing []
      (^str name)
      (^int age)))

Disassembling the above code generates the following Python equivalent.

from dataclasses import dataclass


@dataclass
class Thing:
    description: str
    value: int

Annotations

Supports Python Typing annotations via the special form ^:

  ; 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]] ...)