[英]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.