[英]How to type-hint in Hy
是否可以用 Hy 语言输入提示变量和函数返回值?
# in python we can do this
def some_func() -> str:
return "Hello World"
是的... Hy 至少从 2019 年 10 月 8 日开始实施 PEP 3107 和 526 注释(请参阅此拉取请求: https://github.com/hylang/hy/pull/1810 )
有如下示例中的#^
形式(来自文档: 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))
以及扩展形式的annotate
宏。 还有of
宏(详细在这里https://hyrule.readthedocs.io/en/master/index.html#hyrule.misc.of ):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.