编程语言分类
在编程语言中,类型系统(type system)是非常重要且庞大的内容,本篇只涉及语言强、弱类型和静态、动态类型的粗略划分。
强类型语言
强类型语言(strongly typed language) 是指对变量类型强制要求的语言,比如在变量赋值、函数传参和返回等场景中类型不匹配就会抛错。动态类型语言 也可以是 强类型语言,此时类型检查发生在运行时。
弱类型语言
弱类型语言(weakly typed language) 是指对类型约束更低的语言,在类型不匹配的调用时,会实行隐式类型转换,但可能导致未定义异常或非法值。
静态类型语言
静态类型语言(statically typed language) 是指类型检查发生在编译期,也就是说程序员必须明确指定每个变量的类型。但部分语言提供了 类型推断(type inference) 的能力,但这也表明该变量被编译器认为是特定类型。
动态类型语言
动态类型语言(dynamically typed language) 是指变量类型由运行时决定,类型检查(如果有)发生在运行时。虽然部分 动态类型语言 提供了 类型注解(type annotation) 能力,但并不强制要求。
常见语言分类
Generally, a strongly typed language 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, function return values, procedure arguments and function calling. Dynamically typed languages (where type checking happens at run time) can also be strongly typed. In dynamically typed languages, values, rather than variables, have types.[1]
Wikipedia
A weakly typed language has looser typing rules and may produce unpredictable or even erroneous results or may perform implicit type conversion at runtime.[1:1]
Wikipedia
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is; other languages (e.g.: Java, C, C++) offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin).[2]
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).
Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.
Most dynamically typed languages do allow you to provide type information, but do not require it.[2:1]