3 ms·
That could be a Union[float, list[float]]. Union types are very common! In fact, I think TypeScript will, for your given example, with the 'else' clause accura
by evrimoztamur 3y ago
That could be a Union[float, list[float]]. Union types are very common!
In fact, I think TypeScript will, for your given example, with the 'else' clause accurately identify the type of x to be a float if it's a union like the one I wrote down above.
- taeric 3y agoSorta? The function does return a union type, in isolation. At most callsites, you would know which of the two you are getting. This is much closer to generic invocation, if I remember the name correctly. Was very common in a lot of older dynamic languages. In fact, in a lot of languages, you can't tell this is a float, statically. It would work with whatever type was passed in that supports multiplication. And return the appropriate type. Right?