Chris Angelico writes: > On Sun, Aug 28, 2016 at 2:30 PM, Steve D'Aprano wrote: >> But in dynamic typing, the type information isn't associated with the >> name "x", but with the value 1 currently assigned to it. Change the >> assignment, and the type changes. As a consequence, it is necessary >> to move the type checks from compile time to runtime, but that's not >> the fundamental difference between the two. > > This is where I'm less sure. Sometimes a variable's type should be > broader than just one concrete type - for instance, a variable might > hold 1 over here, and 1.5 over there, and thus is storing either "int > or float" or "any number". If you have a complex hierarchy of types, > how do you know that this variable should be allowed to hold anything > up to a certain level in the hierarchy, and no further?
It's not just literal values that give potential type information in a dynamically typed language. Another source is functions that the compiler knows, and this information propagates back and forth in the analysis of the control flow. For example, below the compiler might infer that x must be a number but not a complex number, then generate one type check (which it might be able to prove redundant) and calls to specialized versions of ceiling and floor. d = ceiling(x) - floor(x) Also known is that the results of the calls are numbers and the difference of numbers is a number, so d gets assigned a number. Perhaps ceiling and floor in the language always return an int. Then d is known to be an int. And so on. I think "soft typing" refers to this kind of work. Scheme implementations have specialized arithmetic operators that require their arguments to be floats. The generic operators do the same thing when their arguments are floats, but the specialized operators provide this type information to an optimizing compiler. This is related to specialized container types that can only contain floats. This way it may be possible to arrange the code so that the compiler knows the type of everything, or almost everything, which helps the compiler in its art. -- https://mail.python.org/mailman/listinfo/python-list