What is the difference between statically typed and dynamically typed languages?

后端 未结 16 2039

I hear a lot that new programming languages are dynamically typed but what does it actually mean when we say a language is dynamically typed vs. statically typed?

相关标签:
16条回答
  • 2020-11-22 01:51

    Compiled vs. Interpreted

    "When source code is translated"

    • Source Code: Original code (usually typed by a human into a computer)
    • Translation: Converting source code into something a computer can read (i.e. machine code)
    • Run-Time: Period when program is executing commands (after compilation, if compiled)
    • Compiled Language: Code translated before run-time
    • Interpreted Language: Code translated on the fly, during execution

    Typing

    "When types are checked"

    5 + '3' is an example of a type error in strongly typed languages such as Go and Python, because they don't allow for "type coercion" -> the ability for a value to change type in certain contexts such as merging two types. Weakly typed languages, such as JavaScript, won't throw a type error (results in '53').

    • Static: Types checked before run-time
    • Dynamic: Types checked on the fly, during execution

    The definitions of "Static & Compiled" and "Dynamic & Interpreted" are quite similar...but remember it's "when types are checked" vs. "when source code is translated".

    You'll get the same type errors irrespective of whether the language is compiled or interpreted! You need to separate these terms conceptually.


    Python Example

    Dynamic, Interpreted

    def silly(a):
        if a > 0:
            print 'Hi'
        else:
            print 5 + '3'
    
    silly(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 5 + '3' 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 languages don't check types until execution, and that line never executes.


    Go Example

    Static, Compiled

    package main
    
    import ("fmt"
    )
    
    func silly(a int) {
      if (a > 0) {
          fmt.Println("Hi")
      } else {
          fmt.Println("3" + 5)
      }
    }
    
    func main() {
      silly(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 even 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 (vs. dynamically); 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 Differences

    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. It typically allows for variables to change types, which can result in unexpected errors.

    0 讨论(0)
  • 2020-11-22 01:51

    Sweet and simple definitions, but fitting the need: Statically typed languages binds the type to a variable for its entire scope (Seg: SCALA) Dynamically typed languages bind the type to the actual value referenced by a variable.

    0 讨论(0)
  • 2020-11-22 01:52

    The terminology "dynamically typed" is unfortunately misleading. All languages are statically typed, and types are properties of expressions (not of values as some think). However, some languages have only one type. These are called uni-typed languages. One example of such a language is the untyped lambda calculus.

    In the untyped lambda calculus, all terms are lambda terms, and the only operation that can be performed on a term is applying it to another term. Hence all operations always result in either infinite recursion or a lambda term, but never signal an error.

    However, were we to augment the untyped lambda calculus with primitive numbers and arithmetic operations, then we could perform nonsensical operations, such adding two lambda terms together: (λx.x) + (λy.y). One could argue that the only sane thing to do is to signal an error when this happens, but to be able to do this, each value has to be tagged with an indicator that indicates whether the term is a lambda term or a number. The addition operator will then check that indeed both arguments are tagged as numbers, and if they aren't, signal an error. Note that these tags are not types, because types are properties of programs, not of values produced by those programs.

    A uni-typed language that does this is called dynamically typed.

    Languages such as JavaScript, Python, and Ruby are all uni-typed. Again, the typeof operator in JavaScript and the type function in Python have misleading names; they return the tags associated with the operands, not their types. Similarly, dynamic_cast in C++ and instanceof in Java do not do type checks.

    0 讨论(0)
  • 2020-11-22 01:54

    Statically typed languages: each variable and expression is already known at compile time.

    (int a; a can take only integer type values at runtime)

    Examples: C, C++, Java

    Dynamically typed languages: variables can receive different values at runtime and their type is defined at run time.

    (var a; a can take any kind of values at runtime)

    Examples: Ruby, Python.

    0 讨论(0)
  • 2020-11-22 01:55

    http://en.wikipedia.org/wiki/Type_system

    Static typing

    A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. In static typing, types are associated with variables not values. Statically typed languages include Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML, Pascal, Perl (with respect to distinguishing scalars, arrays, hashes and subroutines) and Scala. Static typing is a limited form of program verification (see type safety): accordingly, it allows many type errors to be caught early in the development cycle. Static type checkers evaluate only the type information that can be determined at compile time, but are able to verify that the checked conditions hold for all possible executions of the program, which eliminates the need to repeat type checks every time the program is executed. Program execution may also be made more efficient (i.e. faster or taking reduced memory) by omitting runtime type checks and enabling other optimizations.

    Because they evaluate type information during compilation, and therefore lack type information that is only available at run-time, static type checkers are conservative. They will reject some programs that may be well-behaved at run-time, but that cannot be statically determined to be well-typed. For example, even if an expression always evaluates to true at run-time, a program containing the code

    if <complex test> then 42 else <type error>
    

    will be rejected as ill-typed, because a static analysis cannot determine that the else branch won't be taken.[1] The conservative behaviour of static type checkers is advantageous when evaluates to false infrequently: A static type checker can detect type errors in rarely used code paths. Without static type checking, even code coverage tests with 100% code coverage may be unable to find such type errors. Code coverage tests may fail to detect such type errors because the combination of all places where values are created and all places where a certain value is used must be taken into account.

    The most widely used statically typed languages are not formally type safe. They have "loopholes" in the programming language specification enabling programmers to write code that circumvents the verification performed by a static type checker and so address a wider range of problems. For example, Java and most C-style languages have type punning, and Haskell has such features as unsafePerformIO: such operations may be unsafe at runtime, in that they can cause unwanted behaviour due to incorrect typing of values when the program runs.

    Dynamic typing

    A programming language is said to be dynamically typed, or just 'dynamic', when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, types are associated with values not variables. Dynamically typed languages include Groovy, JavaScript, Lisp, Lua, Objective-C, Perl (with respect to user-defined types but not built-in types), PHP, Prolog, Python, Ruby, Smalltalk and Tcl. Compared to static typing, dynamic typing can be more flexible (e.g. by allowing programs to generate types and functionality based on run-time data), though at the expense of fewer a priori guarantees. This is because a dynamically typed language accepts and attempts to execute some programs which may be ruled as invalid by a static type checker.

    Dynamic typing may result in runtime type errors—that is, at runtime, a value may have an unexpected type, and an operation nonsensical for that type is applied. This operation may occur long after the place where the programming mistake was made—that is, the place where the wrong type of data passed into a place it should not have. This makes the bug difficult to locate.

    Dynamically typed language systems, compared to their statically typed cousins, make fewer "compile-time" checks on the source code (but will check, for example, that the program is syntactically correct). Run-time checks can potentially be more sophisticated, since they can use dynamic information as well as any information that was present during compilation. On the other hand, runtime checks only assert that conditions hold in a particular execution of the program, and these checks are repeated for every execution of the program.

    Development in dynamically typed languages is often supported by programming practices such as unit testing. Testing is a key practice in professional software development, and is particularly important in dynamically typed languages. In practice, the testing done to ensure correct program operation can detect a much wider range of errors than static type-checking, but conversely cannot search as comprehensively for the errors that both testing and static type checking are able to detect. Testing can be incorporated into the software build cycle, in which case it can be thought of as a "compile-time" check, in that the program user will not have to manually run such tests.

    References

    1. Pierce, Benjamin (2002). Types and Programming Languages. MIT Press. ISBN 0-262-16209-1.
    0 讨论(0)
  • 2020-11-22 01:56

    Static typed languages (compiler resolves method calls and compile references):

    • usually better performance
    • faster compile error feedback
    • better IDE support
    • not suited for working with undefined data formats
    • harder to start a development when model is not defined when
    • longer compilation time
    • in many cases requires to write more code

    Dynamic typed languages (decisions taken in running program):

    • lower performance
    • faster development
    • some bugs might be detected only later in run-time
    • good for undefined data formats (meta programming)
    0 讨论(0)
提交回复
热议问题