logoalt Hacker News

aw1621107 • yesterday at 9:23 PM • 2 replies • view on HN

Pretty sure Python's union types don't nest, though. If you could nest optionals then I feel that reduces the need for separate sentinel types since e.g., you can distinguish between "value not present" (None) and "null value present" (Some(None))


Replies

amluto • yesterday at 10:12 PM

Indeed. Option[Foo] really just means Foo | None - you can’t have an Option[Foo] at runtime. Fundamentally, Python’s static typing system describes a language that is a sort of mediocre match for Python.

Another fun one is List[Foo]. Runtime Python has lists but you can’t ask a list what type of list it is, and you can’t write isinstance(a, List[Foo]).

ozyschmozy • yesterday at 9:27 PM

Can you explain or give an example of nested optional types? I don't quite see how that makes sense

➕ show 4 replies