Re:Writing Python Like Rust
by hermitdev on Thursday June 29, 2023 @11:46AM
(#36522030)
There is a difference between type _conversion_ and changing the type of a variable. In your example, there's no conversion performed. "a" is dynamically typed, it's type changes with each assignment. The interpreter does not coerce the type at any step.
Your definition of "f" is no different than this in C++:
template<typename T, typename U>
auto f(T x, U y) -> decltype(x + y) { return x + y; }
"f" is duck typed, effectively. It works fine if operator+ is well defined for "x" and "y", otherwise it fails. The difference, though is a compile time error in C++ vs a runtime error in Python (maybe a linter error with appropriate type hints).
Your definition of "f" is no different than this in C++: template<typename T, typename U> auto f(T x, U y) -> decltype(x + y) { return x + y; }
"f" is duck typed, effectively. It works fine if operator+ is well defined for "x" and "y", otherwise it fails. The difference, though is a compile time error in C++ vs a runtime error in Python (maybe a linter error with appropriate type hints).