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 并不 ...
随机推荐
- Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”
一.说明 在使用python3+selenium写自动升级程序的时侯,碰到一个弹出对话框需要点击确认的场景.弹出的对话框如下图所示. 对于弹框各种资料都说通过switch_to.alert属性获取对话 ...
- 最近C#项目中不小心踩的低级坑
都是很基础的错误问题,大部分都是因为不查一下资料就直接根据其它类似语言的经验写代码导致的 1. 一个企业微信上的正常的界面突然不能滚动了 本以为是浏览器代码计算问题,结果发现是JS出错导致. 2. R ...
- router单页面多个标签tags的用法<router-view></router-view>
<keep-alive><router-view :key="path" /></keep-alive>
- Linux学习笔记之安装报错/lib/ld-linux.so.2: bad ELF interpreter
0x00 64位系统中安装了32位程序解决办法 是因为64位系统中安装了32位程序 解决方法: yum install glibc.i686 0x01 解决交叉编译环境错误 yum install l ...
- 示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本
原文:示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本 一.目的:封装了一些控件到自定义的控件库中,方便快速开发 二.实现功能: 基本实现常 ...
- CentOS 7 安装 mysql 5.7.27 for zabbix
本文是因为需要安装zabbix系统,才贴出的此步骤,供自己查阅方便之用: 在安装使用zabbix前,需要先安装数据库,这里使用的是MySQL数据库进行部署,给出安装步骤,大家觉得有用也可收藏: 当然安 ...
- 基于百度地图JavaScript API,员工住址统计
公司一般都有通讯和住址的统计,但是文字化的表格根本就不知道住在哪. 用百度地图就可以轻松解决, 而且公司还经常人员变动,读取excel中的内容,就不用每次还要更改地图文件了. 在遇到需要聚餐在地图中标 ...
- intent 参数的规范
对于采用 intent 参数的 Activity Manager 命令,您可以使用以下选项指定 intent: -a action 指定 intent 操作,如“android.intent.acti ...
- pandas-20 DataFrame()的基本操作
pandas-20 DataFrame()的基本操作 感觉上pandas的DataFrame就像numpy中的矩阵,不过它拥有列名和索引名,实际操作起来会更方便一些. 如: df = pd.read_ ...
- PL/SQL 出现死锁解决办法
转自:https://blog.csdn.net/u013015629/article/details/48005763 在PL/SQL中操作数据表时,长时间没反应,并且编辑某个表中数据时,出现“re ...