类型安全和类型推断之间有什么区别? [英] What is the difference between Type Safety and Type Inference?

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

问题描述

它们有何不同?我有点困惑,因为它们似乎是相似的概念.

How are they different? I get a bit confused because they seem to be similar concepts.

了解它们如何帮助优化编译时间?

How does understanding them help with optimizing compilation time?

推荐答案

从Swift自己的 类型安全

Swift是一种类型安全的语言.类型安全的语言鼓励您清楚代码可以使用的值的类型. 如果您的代码的一部分需要一个字符串,则不能错误地将其传递给Int.

Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects a String, you can’t pass it an Int by mistake.

var welcomeMessage: String
welcomeMessage = 22 // this would create an error because you  
//already specified that it's going to be a String

类型推断

如果您指定所需的值类型,则Swift会使用类型推断来得出适当的类型.通过类型推断,编译器只需检查您提供的值即可在编译代码时自动推断特定表达式的类型.

If you don’t specify the type of value you need, Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

var meaningOfLife = 42 // meaningOfLife is inferred to be of type Int
meaningOfLife = 55 // it Works, because 55 is an Int

安全类型一起输入推断

var meaningOfLife = 42 // 'Type inference' happened here, we didn't specify that this an Int, the compiler itself found out.
meaningOfLife = 55 // it Works, because 55 is an Int
meaningOfLife = "SomeString" // Because of 'Type Safety' ability you will get an 
//error message: 'cannot assign value of type 'String' to type 'Int'' 


具有关联类型的协议的棘手示例:

想象一下以下协议


Tricky example for protocols with associated types:

Imagine the following protocol

protocol Identifiable {
    associatedtype ID
    var id: ID { get set }

}

您将采用这种方式:

struct Person: Identifiable {
    typealias ID = String
    var id: String
}

但是,您也可以像这样采用它:

However you can also adopt it like this:

struct Website: Identifiable {
    var id: URL
}

您可以删除typealias.编译器仍将推断类型.

You can remove the typealias. The compiler will still infer the type.

有关更多信息,请参见泛型-关联类型

For more see Generics - Associated Types

由于Swift的类型推断,您实际上不需要声明一个 Int的具体项目,作为IntStack定义的一部分.因为 IntStack符合Container的所有要求 协议,Swift可以通过以下方式推断要使用的适当项目 查看append(_ :)方法的item参数的类型以及 下标的返回类型.确实,如果您删除了typealias项目 =上面代码中的int行,一切仍然有效,因为很清楚应该为Item使用哪种类型.

Thanks to Swift’s type inference, you don’t actually need to declare a concrete Item of Int as part of the definition of IntStack. Because IntStack conforms to all of the requirements of the Container protocol, Swift can infer the appropriate Item to use, simply by looking at the type of the append(_:) method’s item parameter and the return type of the subscript. Indeed, if you delete the typealias Item = Int line from the code above, everything still works, because it’s clear what type should be used for Item.


优化编译器性能的专业提示:

您的代码执行类型推理的次数越少,则编译速度越快.因此,建议避免使用集合文字.集合获取的时间越长,其类型推断就越慢...


Pro tip to optimize compiler performance:

The less type inference your code has to do the faster it compiles. Hence it's recommended to avoid collection literals. And the longer a collection gets, the slower its type inference becomes...

还不错

let names = ["John", "Ali", "Jane", " Taika"]

let names : [String] = ["John", "Ali", "Jane", " Taika"]

有关更多信息,请参见此答案.

For more see this answer.

另请参见为什么Swift编译时间这么慢?

该解决方案使他的编译时间从10/15秒减少到一秒钟.

The solution helped his compilation time go down from 10/15 seconds to a single second.

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

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