The difference between static and dynamic strongly typed and weakly typed languages

Original: http://www.zhihu.com/question/19918532

 

Some concepts of the type system have different opinions and are confusing in use. Some things are not even strictly defined. The following is a relatively "strict" statement in academia.


1. First define some basic concepts

Program Errors

  • trapped errors . Causes the program to terminate execution, such as dividing by 0, array out-of-bounds access in Java
  • untrapped errors . Execution continues after an error, but arbitrary behavior is possible. Such as buffer overflow in C, Jump to wrong address

Forbidden Behaviours

At language design time, a set of forbidden behaviors can be defined . It must include all unrapped errors, but may include trapped errors.

 

Well behaved、ill behaved

  • well behaved : if the program execution is unlikely to exhibit forbidden behaviors, it is well behaved .
  • ill behaved: otherwise ill behaved...

2. With the above concepts, let's discuss strong and weak types, static and dynamic types

Strong and weak types

  • Strongly typed : A language is strongly typed if all programs in the language are well behaved—that is, forbidden behaviors are impossible.
  • Weakly typed : otherwise weakly typed. For example, the buffer overflow in C language belongs to trapped errors, that is, forbidden behaviors.. Therefore, C is a weak type

The previous person also said that in weakly typed languages, type checking is less strict, for example, it tends to tolerate implicit type conversions. For example, an int in the C language can be turned into a double. The result is that it is easy to produce forbidden behaviours, so it is weakly typed

Dynamic and static types

  • statically typed : a program is statically typed if it rejects ill behaved at compile time;
  • Dynamically typed : Dynamiclly typed if ill behaviors are rejected at runtime.

3. Misunderstandings
Everyone thinks that C language needs to write int a, int b and the like, but Python does not need to write (you can write a, b directly), so C is static, and Python is dynamic. This understanding is not accurate enough. For example, Ocaml is statically typed, but it can also not be written explicitly. .
Ocaml is a static implicit type.

Static types can be divided into two types:

  • If the type is part of the language grammar, it is explicitly typed;
  • Implicity typed if the type is deduced at compile time , such as ML and Haskell


4. Here are some examples
Untyped: Assembly
Weak Type, Static Type: C/C++
Weak Type, Dynamic Type Check: Perl/PHP
Strong Type, Static Type Check: Java/C#
Strong Type, Dynamic Type Check: Python, Scheme
Static Explicit Type: Java/C
Static Implicit Type: Ocaml, Haskell

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325581044&siteId=291194637