Reputation: 385
I was reading http://tratt.net/laurie/research/pubs/html/tratt__dynamically_typed_languages/#x1-50002.3 and it explains that static analysis is not practical approach on a dynamically typed language but the reasons are not very clear. My question is why is static analysis on very practical on a dynamically typed language ?
Upvotes: 2
Views: 756
Reputation: 52008
Say that cond(y)
is a Boolean-valued function. Suppose that in you have the line
x = "Hello World" if cond(y) else [1,2,3]
Where y
is some other variable. How could any static analysis tell if x
was a string or a list of integers? y
isn't known at compile time. The only way to tell is to run the program -- which defeats the purpose of static analysis
A more realistic example:
def squareRoot(x):
if x > 0:
return math.sqrt(x)
else:
return (0,math.sqrt(abs(x))) #representing complex a+bi as (a,b)
this returns either a float or an int,float pair. Thus there is no well-defined returned value. How is static analysis supposed to handle functions of indeterminate return type?
This sort of consideration places a severe restriction on what a static analyzer could hope to accomplish with a dynamically typed language. This doesn't imply that all static analysis of such languages is impossible or worthless. In fact, there are static analysis tools for Python, with Pylint being the most developed. Even there, though, it is surprising how few of Pylint's errors and warnings are specifically about type errors (such as trying to add a string and an integer). Instead, almost all the error and warning codes are more along the lines of "assigning to function call which doesn't return"
.
Upvotes: 2