ericltw / notes

0 stars 1 forks source link

Static/Dynamic vs Strong/Weak Typing #4

Open ericltw opened 6 years ago

ericltw commented 6 years ago

Static/Dynamic Typing

is about when type information is acquired (Either at compile time or at runtime)

Strong/Weak Typing

is about how strictly types are distinguished (e.g. whether the language tries to do an implicit conversion from strings to numbers).

Reference

ericltw commented 6 years ago

Static and dynamic typing

"When types are checked"

Python Example

Dynamic, Interpreted

def foo(a):
    if a > 0:
        print 'Hi'
    else:
        print "3" + 5
foo(2)

Because Python is both interpreted and dynamically typed, it only translates and type-checks code it's executing on. The else block never executes, so "3" + 5 is never even looked at!

What if it was statically typed?

A type error would be thrown before the code is even run. It still performs type-checking before run-time even though it is interpreted.

What if it was compiled ?

The else block would be translated/looked at before run-time, but because it's dynamically typed it wouldn't throw an error! Dynamically typed language don't check types until execution, and that line never executes.

Go Example

Static, Compiled

package main
import ("fmt"
)
func foo(a int) {
  if (a > 0) {
      fmt.Println("Hi")
  } else {
      fmt.Println("3" + 5)
  }
}
func main() {
  foo(2)
}

The types are checked before running (static) and the type error is immediately caught! The types would still be checked before run-time if it was interpreted, having the same result. If it was dynamic, it wouldn't throw any errors though the code would be looked at during compilation.

Performance

A compiled language will have better performance at run-time if it's statically typed because the knowledge of types allows for machine code optimization.

Statically typed languages have better performance at run-time intrinsically due to not needing to check types dynamically while executing (it checks before running).

Similarly, compiled languages are faster at run time as the code has already been translated instead of needing to "interpret"/translate it on the fly.

Note that both compiled and statically typed languages will have a delay before running for translation and type-checking, respectively.

More Different

static typing catches errors early, instead of finding them during execution (especially useful for long programs) It's more "strict" in that it won't allow for type errors anywhere in your program and often prevents variables from changing types, which further defends against unintended errors.

num = 2
num = '3' // ERROR

Dynamic typing is more flexible (which some appreciate) but allows for variables to change types (sometimes creating unexpected errors).

Reference

ericltw commented 6 years ago

Strong and weak typing

In computer programming , programming languages are often colloquially classified as to whether the language's type system makes it strongly typed or weakly typed. Generally, a strongly typed has stricter typing rules at compile time, which implies that errors and exceptions are more likely to happen during compilation. Most of these rules affect variable assignment, return values and function calling. On the other hand, a weakly typed language has looser typing rules and may produce unpredictable results or may perform implicit type conversion at runtime.

Definitions of "strong" or "weak"

A number of different language design decisions have been referred to as evidence of "strong" or "weak" typing. In fact many of these are more accurately understood as the presence or absence of type safety, memory safety, static type-checking, or dynamic type-checking.

Don't use the terms "strong" and "weak" typing, because they don't have a universally agreed on technical. By contrast, static typing means that programs are checked before being executed, and a program might be rejected before it starts. Dynamic typing means that the types of values are checked during execution, and a poorly typed operation might cause the program to halt or otherwise signal an error at run time.

Strong typing generally means that there are no loopholes in the type system, whereas weak typing means the type system can be subverted (invalidating any guarantees). The terms are often used incorrectly to mean static and dynamic typing. To see the different, think of C: the language is type-checked at compiled time (static typing), but there are plenty of loopholes; you can pretty much cast a value of any type to another type of the same size---in particular, you can cast pointer types freely. Pascal was a language that was intended to be strongly typed but famously had an unforeseen loophole: a variant record with no tag.

Reference