简体   繁体   English

为什么类型声明在静态类型语言中很重要?

[英]Why is the declaration of type important in a statically typed language?

I'm trying to understand the benefit of a programming language being statically typed, and through that, I'm wondering why we need to include type in declaration? 我试图理解静态类型的编程语言的好处,并且通过它,我想知道为什么我们需要在声明中包含类型? Does it serve any purpose rather than to make type explicit? 它是出于任何目的而不是使类型明确吗? If this is the case, I don't see the point. 如果是这种情况,我不明白这一点。 I understand that static typing allows for type checking at compile-time, but if we leave out the explicit type declaration, can't Java still infer type during compile-time? 我理解静态类型允许在编译时进行类型检查,但如果我们省略显式类型声明,Java在编译时是否仍然可以推断类型?

For example, let's say we have in Java: 例如,假设我们在Java中:

myClass test = new myClass();

Isn't the type declaration unnecessary here? 这里不需要类型声明吗? If I'm not mistaken, this is static binding, and Java should know test is of type myClass without explicit declaration of type even at compile-time. 如果我没有弄错,这是静态绑定,Java应该知道testmyClass类型,即使在编译时也没有明确的类型声明。

Response to possible duplicate: this is not a question regarding static vs. dynamic type, but rather about type inference in statically typed languages, as explained in the accepted answer. 对可能重复的响应:这不是关于静态与动态类型的问题,而是关于静态类型语言中的类型推断,如接受的答案中所述。

There are statically typed languages that allow you to omit the type declaration. 静态类型语言,让你忽略类型声明。 This is called type inference . 这称为类型推断 The downsides are that it's tougher to design (for the language designers), tougher to implement (for the compiler writers), and can be tougher to understand when something goes wrong (for programmers). 缺点是设计(对于语言设计者来说)更难以实现(对编译器编写者来说更难),并且在出现问题时(对于程序员来说)更难以理解。 The problem with the last one of those is that if many (or all) of your types are inferred, the compiler can't really tell you much more than "the types aren't all consistent" — often via a cryptic message. 最后一个问题是,如果推断出很多(或所有)类型,编译器就不能告诉你比“类型不一致”更多 - 通常是通过一个神秘的消息。

In a trivial case like the one you cite, yes, it's easy. 在一个简单的案例中,如你引用的那个,是的,这很容易。 But as you get farther from the trivial case, the system quickly grows in complexity. 但随着你越来越远离琐碎的情况,系统的复杂性会迅速增加。

Java does actually do a bit of type inference, in very limited forms. Java确实以非常有限的形式进行了一些类型推断。 For instance, in this snippet: 例如,在此代码段中:

List<String> emptyStrings = Collections.emptyList();

... the compiler has inferred that the method call emptyList returns a List<String> , and not just a List<T> where the type T is unspecified. ...编译器已推断方法调用emptyList返回List<String> ,而不仅仅是List<T> ,其中未指定类型T The non-inferred version of that line (which is also valid Java) is: 该行的非推断版本(也是有效的Java)是:

List<String> emptyStrings = Collections.<String> emptyList();

It is necessary. 有必要。 You can have inheritance, where types are necessary. 您可以拥有继承,其中类型是必需的。

For example: 例如:

Building build1 = new House();
Building build2 = new SkyScraper();

It is the same in polymorphism. 多态性是一样的。

You can then collect all Building s to array for example. 然后,您可以将所有Building s收集到数组中。 If there will be one House and one SkyScraper you can't do this. 如果有一个House和一个SkyScraper你就不能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM