On 29/08/2016 03:43, Steve D'Aprano wrote:
Your question seems to be, what happens if you follow that with an
assignment to a different type?
x = 5
some_code(x)
x = "hello world"
Will the type-checker consider that an error ("you're assigning a str to an
int") or will it infer that x is the Union[int, str]?
Surely that depends on the type-checker! I don't think there's any hard and
fast rule about that, but as far as I know, all statically typed languages
consider than an error.
In C, you can write this:
int x;
x = 5;
x = "hello";
With certain compilers (eg. gcc) you only get a warning. (And since I
don't show warnings to avoid inundation, that seems to compile fine for me!)
But you write:
x = 5.1;
with no errors or warnings.
A dynamic type inference
system could easily cope with this:
x = 5
y = x**2
x = "five"
z = x.upper()
and correctly deduce that, at the time of y's assignment,
exponentiation of x was legal, and at the time of z's, uppercasing
was.
Could it? Do you have an example of a language or type-checker that can do
that? This isn't a rhetorical question.
Actually the above example is one of the easiest to perform
type-inference on, where the variable has been assigned a known type on
the previous line!
--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list