On Aug 29, 2019, at 05:25, Philippe Prados <philippe.prados@gmail.com> wrote:
Hello everybody,
Scala 3 propose the a new syntax for Union type. See here. I propose to add a similar syntax in Python.
# Operator for Union assert( int | str == Union[int,str]) assert( int | str | float == Union[int,str,float]) # Operator for Optional assert( ~int == Optional[int]) |
One immediate problem here is that you’d have to add methods to the builtin type type or this would be illegal at runtime. Which means you couldn’t use this feature in Python 3.7, much less 2.7. I’m not sure it maintaining backward compatibility in typing and in mypy is still as important today as it was 5 years ago, but I’m pretty sure it hasn’t been abandoned entirely.
Also, for ~, that seems pretty misleading. | means union, and not just in Python. And I’m pretty sure it’s the most common way to spell union/sum types and related things across languages. So, str|int isn’t just easier to type, it should actually aid comprehension. But ~ means complement, which is a completely different thing from |None. And the most common way to spell Optional as an operator across languages is ?. Of course I wouldn’t actually expect ~a to mean “any type but a”, but only because of the meta-thought that such a declaration would be completely useless so you must have intended something different. So ~int seems like it would actually harm comprehension instead of helping.
Also, IIRC, multiple shorthands for both Union and Optional were suggested back during the original discussion, including str|int and {str,int} (which doesn’t have the backward compatibility problem) and maybe others. If they were all rejected, the reasoning is probably in the list archives or the GitHub issues repo, so if you want to re-suggest one of them, you probably want to find the original rejection and explain why it no longer applies.