强类型语言和静态类型语言有什么区别? [英] What is the difference between a strongly typed language and a statically typed language?

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

问题描述

还有,一个暗示另一个吗?

Also, does one imply the other?

推荐答案

强类型语言和静态类型语言有什么区别?

What is the difference between a strongly typed language and a statically typed language?

静态类型的语言具有类型系统,该类型系统在实现时由实现(编译器或解释器)检查.类型检查拒绝某些程序,并且通过检查的程序通常带有一些保证.例如,编译器保证不对浮点数使用整数算术指令.

A statically typed language has a type system that is checked at compile time by the implementation (a compiler or interpreter). The type check rejects some programs, and programs that pass the check usually come with some guarantees; for example, the compiler guarantees not to use integer arithmetic instructions on floating-point numbers.

强类型"的含义并没有真正的共识,尽管专业文献中使用最广泛的定义是强类型"语言,但是程序员不可能解决由强类型"引起的限制.类型系统.这个术语几乎总是用来描述静态类型的语言.

There is no real agreement on what "strongly typed" means, although the most widely used definition in the professional literature is that in a "strongly typed" language, it is not possible for the programmer to work around the restrictions imposed by the type system. This term is almost always used to describe statically typed languages.

与静态类型相反的是动态类型",这意味着

The opposite of statically typed is "dynamically typed", which means that

  1. 运行时使用的值被分类为类型.
  2. 对于如何使用此类值有一些限制.
  3. 如果违反了这些限制,则该违反将被报告为(动态)类型错误.

例如, Lua (一种动态类型的语言)具有字符串类型,数字类型和布尔类型等等.在Lua中,每个值都完全属于一种类型,但这并不是所有动态类型语言的要求.在Lua中,允许串联两个字符串,但不允许串联一个字符串和一个布尔值.

For example, Lua, a dynamically typed language, has a string type, a number type, and a Boolean type, among others. In Lua every value belongs to exactly one type, but this is not a requirement for all dynamically typed languages. In Lua, it is permissible to concatenate two strings, but it is not permissible to concatenate a string and a Boolean.

与强类型"相对的是弱类型",这意味着您可以解决类型系统.众所周知,C是弱类型的,因为任何指针类型都可以通过强制转换直接转换为任何其他指针类型. Pascal原本是强类型的,但是设计的监督(未标记的变体记录)在类型系统中引入了漏洞,因此从技术上讲它是弱类型的. 真正强类型语言的示例包括CLU,Standard ML和Haskell.实际上,对标准ML进行了几次修订,以消除在广泛部署该语言后发现的类型系统中的漏洞.

The opposite of "strongly typed" is "weakly typed", which means you can work around the type system. C is notoriously weakly typed because any pointer type is convertible to any other pointer type simply by casting. Pascal was intended to be strongly typed, but an oversight in the design (untagged variant records) introduced a loophole into the type system, so technically it is weakly typed. Examples of truly strongly typed languages include CLU, Standard ML, and Haskell. Standard ML has in fact undergone several revisions to remove loopholes in the type system that were discovered after the language was widely deployed.

总的来说,谈论强"和弱"并没有多大用处.类型系统是否存在漏洞与漏洞的确切数量和性质,在实践中出现的可能性以及利用漏洞的后果无关紧要.实际上,最好完全避免使用术语"strong"和"weak" ,因为

Overall, it turns out to be not that useful to talk about "strong" and "weak". Whether a type system has a loophole is less important than the exact number and nature of the loopholes, how likely they are to come up in practice, and what are the consequences of exploiting a loophole. In practice, it's best to avoid the terms "strong" and "weak" altogether, because

  • 业余爱好者经常将其与静态"和动态"混为一谈.

  • Amateurs often conflate them with "static" and "dynamic".

显然,某些人使用弱类型"来谈论隐性转换的相对盛行或不存在.

Apparently "weak typing" is used by some persons to talk about the relative prevalance or absence of implicit conversions.

专业人士无法就术语的确切含义达成共识.

Professionals can't agree on exactly what the terms mean.

总体而言,您不太可能通知或启发受众.

Overall you are unlikely to inform or enlighten your audience.

可悲的事实是,当谈到类型系统时,"strong"和弱"在技术含义上并没有达成共识.在系统中,最好确切讨论提供了什么保证和不提供什么保证. 例如,一个很好的问题是:是否一定要通过调用该类型的构造函数之一来创建给定类型(或类)的每个值?"在C语言中,答案是否定的.在CLU,F#和Haskell中,是的.对于C ++,我不确定—我想知道.

The sad truth is that when it comes to type systems, "strong" and "weak" don't have a universally agreed on technical meaning. If you want to discuss the relative strength of type systems, it is better to discuss exactly what guarantees are and are not provided. For example, a good question to ask is this: "is every value of a given type (or class) guaranteed to have been created by calling one of that type's constructors?" In C the answer is no. In CLU, F#, and Haskell it is yes. For C++ I am not sure—I would like to know.

相比之下,静态类型意味着程序在执行之前先经过检查 ,并且程序可能在启动之前被拒绝. 动态键入表示的类型在执行过程中被检查,并且类型错误的操作可能导致程序停止或发出错误信号在运行时.静态类型化的主要原因是要排除可能具有此类动态类型错误"的程序.

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. A primary reason for static typing is to rule out programs that might have such "dynamic type errors".

一个暗示另一个吗?

Does one imply the other?

在书呆子层面上,不,因为强"一词实际上没有任何意义.但是实际上,人们几乎总是做以下两件事之一:

On a pedantic level, no, because the word "strong" doesn't really mean anything. But in practice, people almost always do one of two things:

  • 他们(错误地)使用强"和弱"来表示静态"和动态",在这种情况下,他们(错误地)将强类型"和静态类型"互换使用.

  • They (incorrectly) use "strong" and "weak" to mean "static" and "dynamic", in which case they (incorrectly) are using "strongly typed" and "statically typed" interchangeably.

它们使用强"和弱"来比较静态类型系统的属性.很少有人听到有人谈论强"或弱"动态类型系统.除了FORTH,它实际上没有任何类型的类型系统,我想不出可以颠覆类型系统的动态类型语言.根据定义,这些检查会进入执行引擎,并且在执行每个操作之前都会对其进行检查.

They use "strong" and "weak" to compare properties of static type systems. It is very rare to hear someone talk about a "strong" or "weak" dynamic type system. Except for FORTH, which doesn't really have any sort of a type system, I can't think of a dynamically typed language where the type system can be subverted. Sort of by definition, those checks are bulit into the execution engine, and every operation gets checked for sanity before being executed.

无论哪种方式,如果一个人称一种语言为强类型",那么这个人很可能在谈论静态类型的语言.

Either way, if a person calls a language "strongly typed", that person is very likely to be talking about a statically typed language.

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

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