Is it possible to type-hint variables and return values of functions in Hy language?
# in python we can do this
def some_func() -> str:
return "Hello World"
Yes... Hy implements PEP 3107 & 526 annotations since at least 8 Oct 2019 (see this pull request: https://github.com/hylang/hy/pull/1810)
There is the #^
form as in the example below (from the documentation: https://docs.hylang.org/en/master/api.html?highlight=annotation##^)
; 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]] ...)
; Function return annotations come before the function name (if it exists)
(defn #^int add1 [#^int x] (+ x 1))
(fn #^int [#^int y] (+ y 2))
and also the extended form annotate
macro. There is also the of
macro (detailed here https://hyrule.readthedocs.io/en/master/index.html#hyrule.misc.of):