On Mon, Jan 10, 2022 at 12:05 PM Steven D'Aprano email@example.com wrote:
On Mon, Jan 10, 2022 at 05:39:42AM +1100, Chris Angelico wrote:
From my understanding, "x->y" would create a Callable if given two *types*, but its meaning if given two other objects is still undefined.
The PEP requires parentheses around the argument list, so that would be a SyntaxError.
That's a small restriction that makes it less clear as an operator, but sure, pretend I wrote "(x)->y" in the examples.
The PEP also states that the arrow syntax would be equivalent to calling Callable. Callable currently enforces that the return type actually is a type, but doesn't check the input types.
(I don't know if that is a deliberate design or an oversight.)
Assuming that it is an oversight, I would expect that only the following values would be legal for the x and y objects:
- a type;
- a string (which gets converted to a ForwardRef);
and anything else would result in a TypeError.
TypeError is absolutely fine; it's still part of the core language syntax.
1 @ 3
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for @: 'int' and 'int'
If I want to create my own class that responds to matmul, I can do that, even though core data types will all TypeError; it's part of the core language and has syntactic, if not semantic, meaning in any context.