“类型安全”是什么意思?


当前回答

类型安全意味着可以分配给程序变量的值集必须符合定义良好且可测试的标准。类型安全变量导致程序更加健壮,因为操作变量的算法可以相信变量只接受定义良好的一组值中的一个。保持这种信任可以确保数据和程序的完整性和质量。

For many variables, the set of values that may be assigned to a variable is defined at the time the program is written. For example, a variable called "colour" may be allowed to take on the values "red", "green", or "blue" and never any other values. For other variables those criteria may change at run-time. For example, a variable called "colour" may only be allowed to take on values in the "name" column of a "Colours" table in a relational database, where "red, "green", and "blue", are three values for "name" in the "Colours" table, but some other part of the computer program may be able to add to that list while the program is running, and the variable can take on the new values after they are added to the Colours table.

Many type-safe languages give the illusion of "type-safety" by insisting on strictly defining types for variables and only allowing a variable to be assigned values of the same "type". There are a couple of problems with this approach. For example, a program may have a variable "yearOfBirth" which is the year a person was born, and it is tempting to type-cast it as a short integer. However, it is not a short integer. This year, it is a number that is less than 2009 and greater than -10000. However, this set grows by 1 every year as the program runs. Making this a "short int" is not adequate. What is needed to make this variable type-safe is a run-time validation function that ensures that the number is always greater than -10000 and less than the next calendar year. There is no compiler that can enforce such criteria because these criteria are always unique characteristics of the problem domain.

Languages that use dynamic typing (or duck-typing, or manifest typing) such as Perl, Python, Ruby, SQLite, and Lua don't have the notion of typed variables. This forces the programmer to write a run-time validation routine for every variable to ensure that it is correct, or endure the consequences of unexplained run-time exceptions. In my experience, programmers in statically typed languages such as C, C++, Java, and C# are often lulled into thinking that statically defined types is all they need to do to get the benefits of type-safety. This is simply not true for many useful computer programs, and it is hard to predict if it is true for any particular computer program.

长和短....您需要类型安全吗?如果是,那么编写运行时函数来确保当变量被赋值时,它符合定义良好的标准。缺点是它使域分析对于大多数计算机程序来说非常困难,因为您必须显式地为每个程序变量定义标准。

其他回答

“类型安全”的编程语言意味着以下几点:

不能从未初始化的变量中读取 数组的索引不能超出它们的边界 不能执行未检查的类型强制转换

概念:

为了像含义一样非常简单的类型安全,它确保变量的类型应该是安全的

没有错误的数据类型,例如不能保存或初始化字符串类型的整数变量 不可访问超出绑定的索引 只允许特定的内存位置

所以这都是关于变量存储类型的安全性。

试试这个解释……

TypeSafe意味着在编译时静态检查变量是否有适当的赋值。例如,考虑一个字符串或整数。这两种不同的数据类型不能交叉赋值(也就是说,不能将整数赋值给字符串,也不能将字符串赋值给整数)。

对于非类型安全的行为,考虑如下:

object x = 89;
int y;

如果你试图这样做:

y = x;

编译器抛出一个错误,表示它不能转换一个系统。对象转换为整数。你需要明确地这样做。一种方法是:

y = Convert.ToInt32( x );

上面的赋值不是类型安全的。类型安全赋值是指类型可以直接相互赋值。

非类型安全的集合在ASP。NET(例如,应用程序、会话和视图状态集合)。关于这些集合的好消息是(尽量减少多个服务器状态管理方面的考虑),您可以在这三个集合中的任何一个中放入几乎任何数据类型。坏消息是:因为这些集合不是类型安全的,所以在取回值时需要适当地强制转换。

例如:

Session[ "x" ] = 34;

工作很好。但是要将整数值赋回,你需要:

int i = Convert.ToInt32( Session[ "x" ] );

阅读泛型,了解该工具帮助您轻松实现类型安全集合的方式。

c#是一种类型安全语言,但请关注有关c# 4.0的文章;有趣的动态可能性正在逼近(c#实际上得到了严格的选项:关闭,这是一件好事吗?我们将会看到)。

来自文科专业而不是计算机科学专业的解释:

当人们说一种语言或语言特性是类型安全的时,他们的意思是该语言将有助于防止,例如,将非整数的东西传递给某个期望整数的逻辑。

例如,在c#中,我将一个函数定义为:

 void foo(int arg)

编译器会阻止我这样做:

  // call foo
  foo("hello world")

在其他语言中,编译器不会阻止我(或者没有编译器…),所以字符串将被传递给逻辑,然后可能会发生一些不好的事情。

类型安全语言试图在“编译时”捕获更多。

缺点是,使用类型安全语言,当你有一个像“123”这样的字符串,你想像整型一样对它进行操作时,你必须写更多的代码来将字符串转换为整型,或者当你有一个像123这样的整型,并且想在一个像“答案是123”这样的消息中使用它时,你必须写更多的代码来将它转换/强制转换为字符串。

类型安全意味着可以分配给程序变量的值集必须符合定义良好且可测试的标准。类型安全变量导致程序更加健壮,因为操作变量的算法可以相信变量只接受定义良好的一组值中的一个。保持这种信任可以确保数据和程序的完整性和质量。

For many variables, the set of values that may be assigned to a variable is defined at the time the program is written. For example, a variable called "colour" may be allowed to take on the values "red", "green", or "blue" and never any other values. For other variables those criteria may change at run-time. For example, a variable called "colour" may only be allowed to take on values in the "name" column of a "Colours" table in a relational database, where "red, "green", and "blue", are three values for "name" in the "Colours" table, but some other part of the computer program may be able to add to that list while the program is running, and the variable can take on the new values after they are added to the Colours table.

Many type-safe languages give the illusion of "type-safety" by insisting on strictly defining types for variables and only allowing a variable to be assigned values of the same "type". There are a couple of problems with this approach. For example, a program may have a variable "yearOfBirth" which is the year a person was born, and it is tempting to type-cast it as a short integer. However, it is not a short integer. This year, it is a number that is less than 2009 and greater than -10000. However, this set grows by 1 every year as the program runs. Making this a "short int" is not adequate. What is needed to make this variable type-safe is a run-time validation function that ensures that the number is always greater than -10000 and less than the next calendar year. There is no compiler that can enforce such criteria because these criteria are always unique characteristics of the problem domain.

Languages that use dynamic typing (or duck-typing, or manifest typing) such as Perl, Python, Ruby, SQLite, and Lua don't have the notion of typed variables. This forces the programmer to write a run-time validation routine for every variable to ensure that it is correct, or endure the consequences of unexplained run-time exceptions. In my experience, programmers in statically typed languages such as C, C++, Java, and C# are often lulled into thinking that statically defined types is all they need to do to get the benefits of type-safety. This is simply not true for many useful computer programs, and it is hard to predict if it is true for any particular computer program.

长和短....您需要类型安全吗?如果是,那么编写运行时函数来确保当变量被赋值时,它符合定义良好的标准。缺点是它使域分析对于大多数计算机程序来说非常困难,因为您必须显式地为每个程序变量定义标准。