type annotation with Optional #2456
-
Allô, I am working with a Python Arcade example. I am not sure how to express one assignment; it includes a type annotation with Optional.
I got this to work.
Is this the best way to handle this or can I use Optional? Merci. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Using
is written
You may also like the macro |
Beta Was this translation helpful? Give feedback.
-
the collection types in python also use this sq bracket syntax for type annotation: a: list[str] = ['3', '2', '6']
b: tuple[int, int] = (2, 1)
c: dict[str, list[int]] = {}
so, rather than a macro for "Optional"; better would be to create a macro for something like "Parametric Type" (edit: yeah, ; (from typing import Optional) ;; Invalid
(import typing [Optional Union])
; (setv (annotate x (get Optional int)) 1)
(setv (annotate x (typeParam Optional int)) 1) (sorry if snake case is not used in hy/lisp) for my above shared example: E&OE: (setv (annotate a (typeParam list str)) ["3" "2" "6"])
(setv (annotate b (typeParam tuple [int int])) #(2 1))
; ain't sure about how to get commas 😅 as typeParam tuple [int int]
; will likely translate to double brackets: b: tuple[[ int, int ]]
; but for sake of illustration - let's SAY that [int int] is the correct syntax
(setv (annotate c (typeParam dict [str (typeParam list int)])) {})
; well, i am clueless here about how to nest it 😅 |
Beta Was this translation helpful? Give feedback.
That's an issue with Python itself, not Hy.
(get x y)
is translated exactly tox[y]
. You can still make a macro with your preferred syntax (or useof
), if you like.