swift的类型推断
类型推断的前提是有待定类型和上下文。
1、由定义推断实现的类型;
2、由赋值推断声明的类型;
3、由实现推断泛型的类型;
Type inference refers to the automatic detection of the data type of an expression in a programming language.
Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. The compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given. In many cases, it is possible to omit type annotations from a program completely if the type inference system is robust enough, or the program or language is simple enough.
https://www.cnblogs.com/feng9exe/p/9151219.html
Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.)
http://www.cnblogs.com/feng9exe/p/9102835.html
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. Swift 4 uses type inference to work out the appropriate type as follows.
https://www.tutorialspoint.com/swift/swift_data_types.htm
Swift Type Inference Closure Constraints
http://rosslebeau.com/2016/swift-type-inference-closure-constraints
It’s rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it’s defined, Swift can almost always infer the type to be used for that constant or variable, as described in Type Safety and Type Inference. In the welcomeMessage example above, no initial value is provided, and so the type of the welcomeMessage variable is specified with a type annotation rather than being inferred from an initial value.
https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html
Because of type inference, Swift requires far fewer type declarations than languages such as C or Objective-C. Constants and variables are still explicitly typed, but much of the work of specifying their type is done for you.
Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.)
https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID322
Inferring Type From Context
Because the sorting closure is passed as an argument to a method, Swift can infer the types of its parameters and the type of the value it returns. The sorted(by:) method is being called on an array of strings, so its argument must be a function of type (String, String) -> Bool. This means that the (String, String) and Bool types do not need to be written as part of the closure expression’s definition. Because all of the types can be inferred, the return arrow (->) and the parentheses around the names of the parameters can also be omitted:
- reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )
It is always possible to infer the parameter types and return type when passing a closure to a function or method as an inline closure expression. As a result, you never need to write an inline closure in its fullest form when the closure is used as a function or method argument.
Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. These optimizations include:
- Inferring parameter and return value types from context
- Implicit returns from single-expression closures
- Shorthand argument names
- Trailing closure syntax
If you use these shorthand argument names within your closure expression, you can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The in keyword can also be omitted, because the closure expression is made up entirely of its body:
- reversedNames = names.sorted(by: { $0 > $1 } )
Here, $0 and $1 refer to the closure’s first and second String arguments.
Operator Methods
There’s actually an even shorter way to write the closure expression above. Swift’s String type defines its string-specific implementation of the greater-than operator (>) as a method that has two parameters of type String, and returns a value of type Bool. This exactly matches the method type needed by the sorted(by:) method. Therefore, you can simply pass in the greater-than operator, and Swift will infer that you want to use its string-specific implementation:
- reversedNames = names.sorted(by: >)
https://docs.swift.org/swift-book/LanguageGuide/Closures.html
swift的类型推断的更多相关文章
- 浅谈Swift集合类型
Swift 的集合表现形式由数组和字典组成.它可以完美的存储任何呢想存储的东西. 数组是一个同类型的序列化列表集合,它用来存储相同类型的不同值.字典也是一个数组,但它的存值方式类似于Map,通过一对一 ...
- Swift语言指南(四)--类型安全和类型推断
原文:Swift语言指南(四)--类型安全和类型推断 Swift是一门类型安全语言,类型安全语言需要代码里值的类型非常明确.如果你的代码中有部分值需要String类型,你就不能错误地传递Int. 鉴于 ...
- WebKit Web Inspector增加覆盖率分析和类型推断功能
WebKit中的Web Inspector(Web检查器)主要用于查看页面源代码.实时DOM层次结构.脚本调试.数据收集等,日前增加了两个十分有用的新功能:覆盖率分析和类型推断.覆盖率分析工具能够可视 ...
- Swift基础类型
1.使用 let 来声明常量,使用 var 来声明变量. 注:你能够在一行中声明多个常量或者多个变量.用逗号隔开. 2.类型标注 假设初始值没有提供足够的信息(或者没有初始值),那你须要在变量后面声明 ...
- 6.Swift教程翻译系列——Swift集合类型
英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 Swift提供数组和字典两种集合类型.用来存储很多值的情况.数组有序的存储一组同 ...
- C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)
#include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...
- TypeScript Type Innference(类型推断)
在这一节,我们将介绍TypeScript中的类型推断.我们将会讨论类型推断需要在何处用到以及如何推断. 基础 在TypeScript中,在几个没有明确指定类型注释的地方将会使用类型推断来提供类型信息. ...
- Swift数字类型之间的转换
Swift数字类型之间的转换Swift是一种安全的语言,对于类型的检查非常严格,不同类型之间不能随便转换.一.整型之间的转换在C和Objective-C等其他语言中,整型之间有两种转换方法:从小范围数 ...
- C++11新特性:自动类型推断和类型获取
声明:本文是在Alex Allain的文章http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-functi ...
随机推荐
- 手机调试 --- 通过chrome测试手机网站
移动端有时候我们要调试手机网站. Chrome怎么调试手机页面呢? 毕竟有时候手机支持的JS度跟PC不一样.最开始就遇见了.手机端浏览器不支持执行string.includes. PC端支持该函数, ...
- Linux下安装SQL Server 2016(安装篇SQL Server on linux)
安装过程 如何安装直接参考这个文章:安装sql server 整个安装过程非常简单. 上面的文档里是通过 sudo 命令,用root身份来执行,不过这里为了简单,就用root账号来安装的. (1)下载 ...
- Java基础——JSP(三)
一. JavaBean 是一种特殊的java类,它遵从一定的设计模式,开发工具和其他组件可以根据这种模式来调用javaBean.它是使用一种符合某些命名方法和设计规范的java类. -- 这个类是可序 ...
- Java 初级面试题及答案
1.Java中的重载与重写有什么区别 重载(Overload)是让类以统一的方式处理不同类型数据的一种手段,实质表现就是多个具有不同的参数个数或者类型的同名函数(返回值类型可随意,不能以返回类型作为重 ...
- JS 创建自定义对象的方式方法
一.概述 还记得刚开始做项目的时候,看到别人封装的js工具类百思不得其解,看来看去看不懂,深挖一下,其实就是自己没有耐下心去看,但是遇到问题不解决,总会遇到的,今天还是遇到了,就去找了找帖子,重新思考 ...
- MySQL的事务的处理
步骤: 1.开启事务 start transaction 当我们开启一个事务的时候,我们对sql的操作都发生在内存中,但是没有真正的反馈到数据库磁盘的文件中! 2.回滚 rollback 回滚,就是恢 ...
- 设计模式(18)--Memento(备忘录模式)--行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式. ...
- 排序算法(2)--Insert Sorting--插入排序[2]--binary insertion sort--折半(二分)插入排序
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.基本思想 二分法插入排序的思想和直接插入一样,只是找合适的插入位置的方式不同,这里是按二分法找到合适的位置,可 ...
- python中read()、readline()、readlines()函数
python文件读read().readline().readlines()对比 目录 一.read方法 二.readline方法 三.readlines方法 正文 读取文件的三个方法:read( ...
- Oracle中Merge的使用
MERGE INTO products p USING product_changes pc ON (p.product_id = pc.product_id) WHEN MATCHED THEN - ...