静态/动态与强/弱

本文翻译自:Static/Dynamic vs Strong/Weak

I see these terms bandied around all over the place in programming and I have a vague notion of what they mean. 我看到这些术语在编程中遍布各地,我对它们的含义有一个模糊的概念。 A search shows me that such things have been asked all over stack overflow in fact. 搜索显示,事实上,这些事情已被问到堆栈溢出。 As far as I'm aware Static/Dynamic typing in languages is subtly different to Strong/Weak typing but what that difference is eludes me. 据我所知,语言中的静态/动态类型与强/弱类型略有不同,但这种差异是我的意思。 Different sources seem to use different meanings or even use the terms interchangeably. 不同的来源似乎使用不同的含义甚至可以互换地使用这些术语。 I can't find somewhere that talks about both and actually spells out the difference. 我找不到谈论两者的地方,实际上说明了差异。 What would be nice is if someone could please spell this out clearly here for me and the rest of the world. 如果有人能够在这里为我和世界其他地方清楚地说明这一点,那会更好。


#1楼

参考:https://stackoom.com/question/9reQ/静态-动态与强-弱


#2楼

Today researching about this subject I came across this great article http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html It cleared up a lot of things for me and I thought It may add to some of the great answers above. 今天关于这个主题的研究我发现了这篇伟大的文章http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html它清理了很多对我而言我认为这可能会增加上面的一些重要答案。

Strong and Weak Typing: 强劲而弱的打字:

Probably the most common way type systems are classified is "strong" or "weak." 可能最常见的类型系统被分类为“强”或“弱”。 This is unfortunate, since these words have nearly no meaning at all. 这是不幸的,因为这些词根本没有任何意义。 It is, to a limited extent, possible to compare two languages with very similar type systems, and designate one as having the stronger of those two systems. 在有限的程度上,可以将两种语言与非常相似的类型系统进行比较,并将其指定为具有这两种系统中较强的一种。 Beyond that, the words mean nothing at all. 除此之外,这些词语毫无意义。

Static and Dynamic Types 静态和动态类型

This is very nearly the only common classification of type systems that has real meaning. 这几乎是具有实际意义的唯一类型系统的常见分类。 As a matter of fact, it's significance is frequently under-estimated [...] Dynamic and static type systems are two completely different things, whose goals happen to partially overlap. 事实上,它的重要性经常被低估[...]动态和静态类型系统是两个完全不同的东西,其目标恰好发生了部分重叠。

A static type system is a mechanism by which a compiler examines source code and assigns labels (called "types") to pieces of the syntax, and then uses them to infer something about the program's behavior. 静态类型系统是一种机制,通过该机制,编译器检查源代码并将标签(称为“类型”)分配给语法片段,然后使用它们来推断有关程序行为的信息。 A dynamic type system is a mechanism by which a compiler generates code to keep track of the sort of data (coincidentally, also called its "type") used by the program. 动态类型系统是一种机制,通过该机制,编译器生成代码以跟踪程序使用的数据类型(巧合地,也称为“类型”)。 The use of the same word "type" in each of these two systems is, of course, not really entirely coincidental; 当然,在这两个系统中使用相同的“类型”一词并非完全巧合; yet it is best understood as having a sort of weak historical significance. 然而,最好将其理解为具有某种微弱的历史意义。 Great confusion results from trying to find a world view in which "type" really means the same thing in both systems. 试图找到一个世界观,其中“类型”在两个系统中实际上意味着相同的东西,会产生很大的混乱。 It doesn't. 它没有。

Explicit/Implicit Types: 显式/隐式类型:

When these terms are used, they refer to the extent to which a compiler will reason about the static types of parts of a program. 当使用这些术语时,它们指的是编译器在何种程度上推断程序的静态类型部分。 All programming languages have some form of reasoning about types. 所有编程语言都有某种形式的推理类型。 Some have more than others. 有些人比其他人更多。 ML and Haskell have implicit types, in that no (or very few, depending on the language and extensions in use) type declarations are needed. ML和Haskell有隐式类型,因为不需要(或者很少,取决于使用的语言和扩展)类型声明。 Java and Ada have very explicit types, and one is constantly declaring the types of things. Java和Ada具有非常明确的类型,并且不断地声明事物的类型。 All of the above have (relatively, compared to C and C++, for example) strong static type systems. 所有上述(相对于C和C ++,例如)强静态类型系统。


#3楼

  • 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 (eg whether the language tries to do an implicit conversion from strings to numbers). 强/弱分类是关于如何区分严格类型(例如,语言是否尝试从字符串到数字进行隐式转换)。

See the wiki-page for more detailed information. 有关更多详细信息,请参阅wiki页面


#4楼

Weak typing means that the type of an object can change depending on context. 弱类型意味着对象的类型可以根据上下文而改变。 For example in a weakly typed language the string "123" may be treated as the number 123 if you add another number to it. 例如,在弱类型语言中,如果向其添加另一个数字,则字符串“123”可被视为数字123。 Examples of languages with weak typing are bash, awk and PHP. 键入弱的语言示例有bash,awk和PHP。

Another kind of weakly typed language is C, where the data at a memory address can be treated as a different type by casting. 另一种弱类型语言是C,其中内存地址处的数据可以通过强制转换视为不同类型。

In a strongly typed language the type of an object does not change - an int is always an int and trying to use it as a string will result in an error. 在强类型语言中,对象的类型不会改变 - int总是一个int,并且尝试将其用作字符串将导致错误。 Both Java and Python are strongly typed. Java和Python都是强类型的。

The difference between dynamic and static typing is when the type rules are enforced. 动态和静态类型之间的区别在于强制执行类型规则。 In a statically typed language the type of every variable and parameter must be declared in the source and is enforced at compile time. 在静态类型语言中,每个变量和参数的类型必须在源中声明,并在编译时强制执行。 In a dynamically typed language the types are only checked when they are used at runtime. 在动态类型语言中,仅在运行时使用类型时才检查类型。 So Java is statically typed and Python is dynamically typed. 所以Java是静态类型的,Python是动态类型的。

However the boundaries can be a little blurry at times. 然而,有时边界可能有点模糊。 For example although Java is statically typed, every time you use reflection or a cast (eg when using containers of Objects) they you are deferring the type check to runtime. 例如,虽然Java是静态类型的,但每次使用反射或强制转换时(例如,当使用对象的容器时),您都会将类型检查推迟到运行时。

Similarly most strongly typed languages will still automatically convert between integers and floats (and in some languages abitrary precision BigInts). 类似地,大多数强类型语言仍将自动在整数和浮点数之间进行转换(在某些语言中,精确的BigInts)。


#5楼

You have discovered a soft spot in the terminology that amateurs use to talk about programming languages. 你已经发现业余爱好者用来谈论编程语言的术语情有独钟。 Don't use the terms "strong" and "weak" typing , because they don't have a universally agreed on technical meaning. 不要使用“强”和“弱”类型的术语 ,因为它们没有普遍认同的技术含义。 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". 静态类型的主要原因是排除可能具有此类“动态类型错误”的程序。

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 difference, think of C: the language is type-checked at compile time (static typing), but there are plenty of loopholes; 为了看到差异,想想C:语言在编译时被类型检查(静态类型),但是存在很多漏洞; 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. Pascal是一种旨在强类型的语言,但着名的是有一个无法预料的漏洞:一个没有标签的变体记录。

Implementations of strongly typed languages often acquire loopholes over time, usually so that part of the run-time system can be implemented in the high-level language. 强类型语言的实现通常会随着时间的推移而获得漏洞,通常是因为部分运行时系统可以用高级语言实现。 For example, Objective Caml has a function called Obj.magic which has the run-time effect of simply returning its argument, but at compile time it converts a value of any type to one of any other type. 例如,Objective Caml有一个名为Obj.magic的函数,它具有简单地返回其参数的运行时效果,但在编译时它将任何类型的值转换为任何其他类型的值。 My favorite example is Modula-3, whose designers called their type-casting construct LOOPHOLE . 我最喜欢的例子是Modula-3,其设计师将其类型转换构造称为LOOPHOLE

Having said that, you can't count on any two people using the words "strong" and "weak" in exactly the same way. 话虽如此,你不能指望任何两个人以完全相同的方式使用“强”和“弱”这两个词。 So avoid them. 所以避免他们。


#6楼

Simply put it this way: in a statically typed language the type is static , meaning once you set a variable to a type, you CANNOT change it. 简单地说就是这样:在静态类型语言中 ,类型是静态的 ,这意味着一旦将变量设置为类型,就无法更改它。 That is because typing is associated with the variable rather than the value it refers to. 这是因为键入与变量而不是它所引用的值相关联。

For example in Java: 例如在Java中:

String str = "Hello";  //statically typed as string
str = 5;               //would throw an error since java is statically typed

Whereas in a dynamically typed language the type is dynamic , meaning after you set a variable to a type, you CAN change it. 而在动态类型语言中 ,类型是动态的 ,这意味着在将变量设置为类型后,您可以更改它。 That is because typing is associated with the value rather than the variable. 这是因为键入与值而不是变量相关联。

For example in Python: 例如在Python中:

str = "Hello" # it is a string
str = 5       # now it is an integer; perfectly OK

On the other hand, the strong/weak typing in a language is related to implicit type conversions (partly taken from @Dario's answer): 另一方面,语言中的强/弱类型与隐式类型转换有关(部分取自@Dario的答案):

For example in Python: 例如在Python中:

str = 5 + "hello" 
# would throw an error since it does not want to cast one type to the other implicitly. 

whereas in PHP: 而在PHP中:

$str = 5 + "hello"; // equals 5 because "hello" is implicitly casted to 0 
// PHP is weakly typed, thus is a very forgiving language.

Static typing allows for checking type correctness at compile time. 静态类型允许在编译时检查类型的正确性。 Statically typed languages are usually compiled, and dynamically typed languages are interpreted. 通常编译静态类型语言,并解释动态类型语言。 Therefore, dynamicly typed languages can check typing at run time. 因此,动态类型语言可以在运行时检查键入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值