静态类型的语言和动态类型的语言有什么区别? [英] What is the difference between statically typed and dynamically typed languages?

查看:233
本文介绍了静态类型的语言和动态类型的语言有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到很多新的编程语言是动态类型的,但是当我们说一种语言是动态类型还是静态类型时,这实际上意味着什么?

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?

推荐答案

静态类型的语言

如果在编译时就知道了变量的类型,则将使用静态类型的语言.对于某些语言,这意味着您作为程序员必须指定每个变量的类型(例如:Java,C,C ++);其他语言提供某种形式的类型推断,即类型系统推断变量类型的能力(例如:OCaml,Haskell,Scala,Kotlin)

Statically typed languages

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 (e.g.: Java, C, C++); other languages 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)

这里的主要优点是编译器可以完成各种检查,因此在很早的阶段就捕获了许多琐碎的错误.

The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.

示例:C,C ++,Java,Rust,Go,Scala

Examples: C, C++, Java, Rust, Go, Scala

如果一种语言与运行时值相关联,而不是命名为变量/字段/等,则该语言是动态键入的.这意味着作为程序员,您可以更快地编写代码,因为您不必每次都指定类型(除非使用带有 type inference 的静态类型的语言).

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).

示例:Perl,Ruby,Python,PHP,JavaScript

Examples: Perl, Ruby, Python, PHP, JavaScript

大多数脚本语言都具有此功能,因为仍然没有编译器执行静态类型检查,但是您可能会发现自己正在寻找由于解释器错误解释变量类型而导致的错误.幸运的是,脚本通常很小,因此错误没有太多隐藏的地方.

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.

大多数动态类型化的语言都允许您提供类型信息,但不需要.目前正在开发的一种语言 Rascal 采用一种混合方法,允许在函数内进行动态键入但强制执行静态输入功能签名.

Most dynamically typed languages do allow you to provide type information, but do not require it. One language that is currently being developed, Rascal, takes a hybrid approach allowing dynamic typing within functions but enforcing static typing for the function signature.

这篇关于静态类型的语言和动态类型的语言有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆