The Secret Life of Types in Swift

At first glance, Swift looked different as a language compared to Objective-C because of the modern code syntax. Secondly, the strict and strong type system is also different.
This talk takes an under-the-hood deep dive into the Swift type system’s structure and gives tips how to use it in a proper way.
Introduction
I’m Manu, and I will talk to you about data types today. Swift is pretty simple compared to other languages, and Swift is pretty clear in its architecture of the typing system.
It’s All About Types
There are two things to know first. First, Swift says there shall not be root - $noRoot. And we will never have nil.

There’s no specific root type in Swift. For example, in other languages like Java, or C#, or static type systems, they have a root type. Swift does not have a root type, and empty values are not allowed.
In Java, an Integer is just a number. The inheritance line is not long, but we do have two stages of inheritance over the type itself, with interfaces, which conforms to a protocol. Objective-C is similar, as it also conforms to a protocol.

In Swift, Int is a type of itself, and it defines its data types as structures, not classes.
Dive into Types
In Swift, everything is a structure with no inheritance. The reason this is done is so we can have super loose coupling among each type, and can be extended throughout the whole type system. This allows for clean architecture.
Named Types

Named types are types that have names. It’s classes, structures, Enums, and protocols; essentially everything you are giving a name when you are using it.
Compound Types

A compound type is a type with no name. The two types of compound types are tuples and functions.
Tuples
This greatTuple variable has exactly the type Int, Int. This is a real type, it just looks like a bit different.
You can attach, or assign a different Int, Int tuple. To do so, leave the parameters, and use the numbers themselves. You can enter the values with the .0, and .1, and so on. Or, you can use the parameter names again.
Let’s continue with tuples, and where they are really meaningful.
Suppose we want to return more than one value from functions. This is done by just defining the types in the return as a tuple.
Tuple types are unnamed but you can give them a name. With the typealias, you can do exactly this.
Here, we are defining a tuple of just two Ints. There is a variable named Point.
Function Types
Like tuples, functions types are also common.
If I want to return nothing, you can get by with writing nothing. The thing exactly after the name of the function, until the first bracket, is the function type.
https://academy.realm.io/posts/altconf-2017-manu-rink-secret-life-of-types-in-swift/?w=1
The Secret Life of Types in Swift的更多相关文章
- iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)
值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift学习笔记1——基础部分
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- 理解 OpenStack Swift (1):OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置
本系列文章着重学习和研究OpenStack Swift,包括环境搭建.原理.架构.监控和性能等. (1)OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置 ( ...
- Swift互用性:与 Cocoa 数据类型共舞(Swift 2.0版)-b
本节内容包括: 字符串(Strings) 数值(Numbers) 集合类(Collection Classes) 错误(Errors) Foundation数据类型(Foundation Data T ...
- Swift: Associated Types--为什么协议使用关联类型而不是泛型
关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...
- swift,NSUserDefaults的swift化封装
NSUserDefaultshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror- ...
- swift内存管理:值类型与引用类型
Use struct to create a structure. Structures support many of the same behaviors as classes, includin ...
- 行走于Swift的世界中(转)
从Swift正式公布到现在,我基本一直在关注和摸索Swift.对于一门新语言来说,开荒阶段的探索自然是激动人心的,但是很多时候,资料的缺失和细节的隐藏也让人着实苦恼.最近几天的感受是,Swift 并不 ...
随机推荐
- xunsearch搜索测试
1.导入测试数据 cd $prefix/sdk/php util/Indexer.php --source=csv --clean demo 然后出现 初始化数据源 ... csv WARNING: ...
- c++作业题sin公式
今日 有一位同样读大一的朋友向我求助有关c++的作业题 他说他的程序逻辑正确 但是结果的精度不对 题目如下: 这是一道看起来十分简单的作业题 我按照要求快速地写了一个版本 不出所料 一样遇到了精度问题 ...
- ubuntu docker 搭建 mongodb,开启授权访问 redis,mysql mssql 备份还原
命令安装docker 如果您想从Ubuntu存储库安装docker版本,则可以运行下面的apt命令. sudo apt install docker.io等到安装完成后,您可以启动Docker并使用s ...
- 「NOI2015」小园丁与老司机
「NOI2015」小园丁与老司机 要不是这道码农题,去年就补完了NOI2015,其实两问都比较simple,但是写起来很恶心. 先解决第一问,记 \(dp[i]\) 表示老司机到达第 \(i\) 棵树 ...
- Java学习:递归
递归的思想 以此类推是递归的基本思想. 具体来讲就是把规模大的问题转化为规模小的相似的子问题来解决.在函数实现时,因为解决大问题的方法和解决小问题的方法往往是同一个方法,所以就产生了函数调用它自身的情 ...
- Elasticsearch 史上最全最常用工具清单
基础类工具 1.Head插件 1)功能概述: ES集群状态查看.索引数据查看.ES DSL实现(增.删.改.查操作) 比较实用的地方:json串的格式化 2)地址:http://mobz.github ...
- MySQL卸载与重装
[卸载] 不推荐使用控制面板-->卸载程序,来卸载,容易出现报错. 使用电脑管家卸载很轻松. [删除注册列表] HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\S ...
- css 光标
<style> div{width:100;height:50;float:left;border:1px solid red;margin:1px;} </style> &l ...
- 关于使用mySqlSugar插入数据异常解决方案
项目的解决方案中引用的有mysqlsugar的数据库操作库,在使用插入数据过程中一些特殊的生僻字或表情符号总会提示: Incorrect string value: '\xF0\x9F...' for ...
- asp.net core 系列之允许跨域访问2之测试跨域(Enable Cross-Origin Requests:CORS)
这一节主要讲如何测试跨域问题 你可以直接在官网下载示例代码,也可以自己写,我这里直接使用官网样例进行演示 样例代码下载: Cors 一.提供服务方,这里使用的是API 1.创建一个API项目.或者直接 ...