1

Recently I'm working on learning the dynamic and static analysis of language. I've read some related question like static and dynamic code analysis but I still have some questions.

Dynamic analysis can happen during compile and runtime period and check the program status. With dynamic analysis, unwanted execution step can be avoided. Static analysis often refers to static type check system and a static analysis must correctly predict the actual program behavior. For static type systems, this means that the assigned type must describe all values that a program may evaluate to at runtime. I'm wondering what happens if a type checker assigns a type to a program that does not describe all values that the program evaluates to. Can anybody give an example of that?

Coding_Rabbit
  • 1,287
  • 3
  • 22
  • 44

1 Answers1

0

In statically typed systems values cannot be assigned (or passed as parameters) if they are of a type incompatible with that of the target (though most languages provide a way to force the assignment).

Static type analysis of statically typed languages is not predictive; it's prescriptive.

But static analysis can do predictive analysis too, like checking possible access through a null value, or not handling al values of an enum in a switch/case statement, or conditionals over conditions that are always true or `false, etc. There are many common programming errors that can be detected by static analysis.

Static type analysis of dynamically typed languages is also possible by using type annotations and type inference.

Apalala
  • 9,017
  • 3
  • 30
  • 48
  • I would remove that last sentence. _type inference_ is generally used to assign types during static analysis of statically typed languages. While this may also be possible for dynamically typed languages (I believe some Ruby type checkers do this) it does not imply a dynamically typed language. eg Scala and Elm are both statically typed and have type inference – Steven Noble Feb 09 '19 at 09:12
  • 1
    Not remove, but rephrase @StevenNoble. I agree that, as it is, it is implied that type inference is exclusive to dynamically typed languages. – Apalala Feb 12 '19 at 13:50