“类型安全”和“强类型”是否意味着同样的事情?

时间:2021-07-28 16:02:06

Do "type-safe" and "strongly typed" mean the same thing?

“类型安全”和“强类型”是否意味着同样的事情?

5 个解决方案

#1


No, not necessarily - although it depends on your definition of the terms, and there are no very clear and widely accepted definitions.

不,不一定 - 虽然这取决于您对术语的定义,但没有非常明确和广泛接受的定义。

For instance, dynamic programming languages are often type safe, but not strongly typed. In other words, there's no compile-time type information determining what you can and can't do with a type, but at execution time the runtime makes sure you don't use one type as if it were another.

例如,动态编程语言通常是类型安全的,但不是强类型的。换句话说,没有编译时类型信息确定您可以和不能对类型执行什么操作,但是在执行时,运行时会确保您不使用一种类型,就好像它是另一种类型一样。

For example, in C# 4.0, you can do:

例如,在C#4.0中,您可以执行以下操作:

dynamic foo = "hello";
dynamic length = foo.Length; // Uses String.Length at execution time
foo = new int[] { 10, 20, 30 };
length = foo.Length; // Uses Array.Length at execution time
dynamic bar = (FileStream) foo; // Fails!

The last line is the key to it being type-safe: there's no safe conversion from an int array to a FileStream, so the operation fails - instead of treating the bytes of the array object as if they were a FileStream.

最后一行是类型安全的关键:没有从int数组到FileStream的安全转换,因此操作失败 - 而不是将数组对象的字节视为FileStream。

EDIT: C# is normally both "strongly typed" (as a language) and type safe: the compiler won't let you attempt to make arbitrary calls on an object, and the runtime won't let you perform inappropriate conversions.

编辑:C#通常都是“强类型”(作为一种语言)和类型安全:编译器不会让你尝试对对象进行任意调用,运行时不会让你执行不适当的转换。

I'm not entirely sure where unsafe code fits in - I don't know enough about it to comment, I'm afraid.

我不完全确定哪些不安全的代码适合 - 我不太了解它的评论,我很害怕。

Dynamic typing in C# 4 allows weakly typed but still type-safe code, as shown above.

C#4中的动态类型允许弱类型但仍然是类型安全的代码,如上所示。

Note that foreach performs an implicit conversion, making it a sort of hybrid:

请注意,foreach执行隐式转换,使其成为一种混合:

ArrayList list = new ArrayList();
list.Add("foo");

foreach (FileStream stream in list)
{
    ...
}

This will compile (there was another question on this recently) but will fail at execution time. Ironically, that's because you're trying to be strongly typed with respect to the stream variable, which means you have to perform a cast on the result of the iterator.

这将编译(最近有另一个问题)但在执行时会失败。具有讽刺意味的是,这是因为您尝试对流变量进行强类型化,这意味着您必须对迭代器的结果执行强制转换。

#2


Good question. Read this wikipedia entry, here's an extract:

好问题。阅读这个*条目,这是一个摘录:

Benjamin C. Pierce, author of Types and Programming Languages and Advanced Types and Programming Languages, says, "I spent a few weeks... trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless."

Benjamin C. Pierce,“类型和编程语言以及高级类型和编程语言”一书的作者说:“我花了几周时间......试图理清”强类型“,”静态类型“,”安全“等术语。等等,发现它非常困难......这些术语的使用是多种多样的,以至于使它们几乎无用。“

#3


"Type Safe" means that there's no casting involved and no run-time type errors can occur.

“类型安全”表示不涉及转换,也不会发生运行时类型错误。

Some people argue that "Strongly Typed" mean nothing, or "it's good", or "i'm comfortable with it".

有些人认为“强烈打字”意味着什么,或“它很好”,或者“我很满意”。

Anyway, "Type Safe" relates to a portion of code or API, when "Strongly Typed" refers to a whole language or platform.

无论如何,“类型安全”涉及代码或API的一部分,当“强类型”指整个语言或平台时。

#4


ype safe means preventing programs from accessing memory outside the bounds of an object's public properties. When code is not type-safe, unwanted side effects can occur. Type-safety is important for assembly isolation and security enforcement. When code is type- safe, the common language runtime can completely isolate assemblies from each other

ype安全意味着阻止程序访问对象公共属性范围之外的内存。当代码不是类型安全的时,可能会发生不必要的副作用。类型安全对于程序集隔离和安全实施非常重要。当代码是类型安全的时,公共语言运行库可以完全隔离组件

#5


They are the basically same, it's just a matter of interpretation:

它们基本相同,只是解释的问题:

From wikipedia:

Type safety:

Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible. A dynamically typed language such as Smalltalk can be seen as a strongly typed language with a very permissive type system where any syntactically correct program is well-typed; as long as its dynamic semantics ensures that no such program ever "goes wrong" in an appropriate sense, it satisfies the definition above and can be called type-safe.

类型安全性与强类型的许多定义之一是同义词;但类型安全和动态类型是相互兼容的。动态类型语言(如Smalltalk)可以看作是一种强类型语言,具有非常宽松的类型系统,其中任何语法正确的程序都是良好类型的;只要它的动态语义确保没有这样的程序在适当的意义上“出错”,它就满足上面的定义并且可以称为类型安全的。

#1


No, not necessarily - although it depends on your definition of the terms, and there are no very clear and widely accepted definitions.

不,不一定 - 虽然这取决于您对术语的定义,但没有非常明确和广泛接受的定义。

For instance, dynamic programming languages are often type safe, but not strongly typed. In other words, there's no compile-time type information determining what you can and can't do with a type, but at execution time the runtime makes sure you don't use one type as if it were another.

例如,动态编程语言通常是类型安全的,但不是强类型的。换句话说,没有编译时类型信息确定您可以和不能对类型执行什么操作,但是在执行时,运行时会确保您不使用一种类型,就好像它是另一种类型一样。

For example, in C# 4.0, you can do:

例如,在C#4.0中,您可以执行以下操作:

dynamic foo = "hello";
dynamic length = foo.Length; // Uses String.Length at execution time
foo = new int[] { 10, 20, 30 };
length = foo.Length; // Uses Array.Length at execution time
dynamic bar = (FileStream) foo; // Fails!

The last line is the key to it being type-safe: there's no safe conversion from an int array to a FileStream, so the operation fails - instead of treating the bytes of the array object as if they were a FileStream.

最后一行是类型安全的关键:没有从int数组到FileStream的安全转换,因此操作失败 - 而不是将数组对象的字节视为FileStream。

EDIT: C# is normally both "strongly typed" (as a language) and type safe: the compiler won't let you attempt to make arbitrary calls on an object, and the runtime won't let you perform inappropriate conversions.

编辑:C#通常都是“强类型”(作为一种语言)和类型安全:编译器不会让你尝试对对象进行任意调用,运行时不会让你执行不适当的转换。

I'm not entirely sure where unsafe code fits in - I don't know enough about it to comment, I'm afraid.

我不完全确定哪些不安全的代码适合 - 我不太了解它的评论,我很害怕。

Dynamic typing in C# 4 allows weakly typed but still type-safe code, as shown above.

C#4中的动态类型允许弱类型但仍然是类型安全的代码,如上所示。

Note that foreach performs an implicit conversion, making it a sort of hybrid:

请注意,foreach执行隐式转换,使其成为一种混合:

ArrayList list = new ArrayList();
list.Add("foo");

foreach (FileStream stream in list)
{
    ...
}

This will compile (there was another question on this recently) but will fail at execution time. Ironically, that's because you're trying to be strongly typed with respect to the stream variable, which means you have to perform a cast on the result of the iterator.

这将编译(最近有另一个问题)但在执行时会失败。具有讽刺意味的是,这是因为您尝试对流变量进行强类型化,这意味着您必须对迭代器的结果执行强制转换。

#2


Good question. Read this wikipedia entry, here's an extract:

好问题。阅读这个*条目,这是一个摘录:

Benjamin C. Pierce, author of Types and Programming Languages and Advanced Types and Programming Languages, says, "I spent a few weeks... trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless."

Benjamin C. Pierce,“类型和编程语言以及高级类型和编程语言”一书的作者说:“我花了几周时间......试图理清”强类型“,”静态类型“,”安全“等术语。等等,发现它非常困难......这些术语的使用是多种多样的,以至于使它们几乎无用。“

#3


"Type Safe" means that there's no casting involved and no run-time type errors can occur.

“类型安全”表示不涉及转换,也不会发生运行时类型错误。

Some people argue that "Strongly Typed" mean nothing, or "it's good", or "i'm comfortable with it".

有些人认为“强烈打字”意味着什么,或“它很好”,或者“我很满意”。

Anyway, "Type Safe" relates to a portion of code or API, when "Strongly Typed" refers to a whole language or platform.

无论如何,“类型安全”涉及代码或API的一部分,当“强类型”指整个语言或平台时。

#4


ype safe means preventing programs from accessing memory outside the bounds of an object's public properties. When code is not type-safe, unwanted side effects can occur. Type-safety is important for assembly isolation and security enforcement. When code is type- safe, the common language runtime can completely isolate assemblies from each other

ype安全意味着阻止程序访问对象公共属性范围之外的内存。当代码不是类型安全的时,可能会发生不必要的副作用。类型安全对于程序集隔离和安全实施非常重要。当代码是类型安全的时,公共语言运行库可以完全隔离组件

#5


They are the basically same, it's just a matter of interpretation:

它们基本相同,只是解释的问题:

From wikipedia:

Type safety:

Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible. A dynamically typed language such as Smalltalk can be seen as a strongly typed language with a very permissive type system where any syntactically correct program is well-typed; as long as its dynamic semantics ensures that no such program ever "goes wrong" in an appropriate sense, it satisfies the definition above and can be called type-safe.

类型安全性与强类型的许多定义之一是同义词;但类型安全和动态类型是相互兼容的。动态类型语言(如Smalltalk)可以看作是一种强类型语言,具有非常宽松的类型系统,其中任何语法正确的程序都是良好类型的;只要它的动态语义确保没有这样的程序在适当的意义上“出错”,它就满足上面的定义并且可以称为类型安全的。