什么是类型安全? [英] What is Type-safe?

查看:60
本文介绍了什么是类型安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型安全"是什么意思?

What does "type-safe" mean?

推荐答案

类型安全意味着编译器将在编译时验证类型,如果尝试将错误的类型分配给变量,则会引发错误.

Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.

一些简单的例子:

// Fails, Trying to put an integer in a string
String one = 1;
// Also fails.
int foo = "bar";

这也适用于方法参数,因为您要将显式类型传递给它们:

This also applies to method arguments, since you are passing explicit types to them:

int AddTwoNumbers(int a, int b)
{
    return a + b;
}

如果我尝试使用以下方式拨打电话:

If I tried to call that using:

int Sum = AddTwoNumbers(5, "5");

编译器将引发错误,因为我传递的是字符串("5"),并且它期望使用整数.

The compiler would throw an error, because I am passing a string ("5"), and it is expecting an integer.

在松散类型的语言(例如javascript)中,我可以执行以下操作:

In a loosely typed language, such as javascript, I can do the following:

function AddTwoNumbers(a, b)
{
    return a + b;
}

如果我这样称呼它:

Sum = AddTwoNumbers(5, "5");

Javascript自动将5转换为字符串,并返回"55".这是由于javascript使用+号进行字符串连接.要使其具有类型意识,您需要执行以下操作:

Javascript automaticly converts the 5 to a string, and returns "55". This is due to javascript using the + sign for string concatenation. To make it type-aware, you would need to do something like:

function AddTwoNumbers(a, b)
{
    return Number(a) + Number(b);
}

或者,也许:

function AddOnlyTwoNumbers(a, b)
{
    if (isNaN(a) || isNaN(b))
        return false;
    return Number(a) + Number(b);
}

如果我这样称呼它:

Sum = AddTwoNumbers(5, " dogs");

Javascript自动将5转换为字符串,并将其附加,以返回"5条狗".

Javascript automatically converts the 5 to a string, and appends them, to return "5 dogs".

并非所有动态语言都像javascript一样宽容(实际上,动态语言并不隐式暗示使用松散类型的语言(请参阅Python)),其中一些实际上会在无效类型转换时给您带来运行时错误.

Not all dynamic languages are as forgiving as javascript (In fact a dynamic language does not implicity imply a loose typed language (see Python)), some of them will actually give you a runtime error on invalid type casting.

尽管它很方便,但它使您容易陷入很多错误,而这些错误很容易被遗漏,并且只能通过测试正在运行的程序来识别.就个人而言,我更愿意让编译器告诉我是否犯了这个错误.

While its convenient, it opens you up to a lot of errors that can be easily missed, and only identified by testing the running program. Personally, I prefer to have my compiler tell me if I made that mistake.

现在,回到C#...

Now, back to C#...

C#支持一种称为 covariance 的语言功能,表示您可以将基本类型替换为子类型,而不会引起错误,例如:

C# supports a language feature called covariance, this basically means that you can substitute a base type for a child type and not cause an error, for example:

 public class Foo : Bar
 {
 }

在这里,我创建了一个新类(Foo),该类继承了Bar.我现在可以创建一个方法:

Here, I created a new class (Foo) that subclasses Bar. I can now create a method:

 void DoSomething(Bar myBar)

并使用Foo或Bar作为参数来调用它,两者都将起作用而不会引起错误.之所以可行,是因为C#知道Bar的任何子类都将实现Bar的接口.

And call it using either a Foo, or a Bar as an argument, both will work without causing an error. This works because C# knows that any child class of Bar will implement the interface of Bar.

但是,您不能做相反的事情:

However, you cannot do the inverse:

void DoSomething(Foo myFoo)

在这种情况下,我无法将Bar传递给此方法,因为编译器不知道Bar实现Foo的接口.这是因为子类可以(并且通常会)与父类大不相同.

In this situation, I cannot pass Bar to this method, because the compiler does not know that Bar implements Foo's interface. This is because a child class can (and usually will) be much different than the parent class.

当然,现在我已经走了很深的距离,超出了原始问题的范围,但是它是所有要了解的好东西:)

Of course, now I've gone way off the deep end and beyond the scope of the original question, but its all good stuff to know :)

这篇关于什么是类型安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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