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

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的更多相关文章

  1. iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)

    值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...

  2. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  3. swift学习笔记1——基础部分

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  4. 理解 OpenStack Swift (1):OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置

    本系列文章着重学习和研究OpenStack Swift,包括环境搭建.原理.架构.监控和性能等. (1)OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置 ( ...

  5. Swift互用性:与 Cocoa 数据类型共舞(Swift 2.0版)-b

    本节内容包括: 字符串(Strings) 数值(Numbers) 集合类(Collection Classes) 错误(Errors) Foundation数据类型(Foundation Data T ...

  6. Swift: Associated Types--为什么协议使用关联类型而不是泛型

    关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...

  7. swift,NSUserDefaults的swift化封装

    NSUserDefaultshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror- ...

  8. swift内存管理:值类型与引用类型

    Use struct to create a structure. Structures support many of the same behaviors as classes, includin ...

  9. 行走于Swift的世界中(转)

    从Swift正式公布到现在,我基本一直在关注和摸索Swift.对于一门新语言来说,开荒阶段的探索自然是激动人心的,但是很多时候,资料的缺失和细节的隐藏也让人着实苦恼.最近几天的感受是,Swift 并不 ...

随机推荐

  1. something want to write

    1.时间戳不能相信是因为机器时间有误差.相当于机器不断电的跑着时钟. 2.写log的时候记得log别人的ip,不然没办法很好的debug.

  2. HTML5 Canvas实战之刮奖效果【转】

    开源项目地址:https://github.com/artwl/Lottery 作者博客地址:http://www.cnblogs.com/jscode/p/3580878.html 谢谢浏览!

  3. Centos 使用kubeadm安装Kubernetes 1.15.3

    本来没打算搞这个文章的,第一里面有瑕疵(没搞定的地方),第二在我的Ubuntu 18 Kubernetes集群的安装和部署 以及Helm的安装 也有安装,第三 和社区的问文章比较雷同 https:// ...

  4. Python协程介绍(转)

    原文:https://www.liaoxuefeng.com/wiki/897692888725344/923057403198272 协程,又称微线程,纤程.英文名Coroutine. 协程的概念很 ...

  5. SpringCloud面试题及答案

    1. 什么是 spring cloud? spring cloud 是一系列框架的有序集合.它利用 spring boot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心. ...

  6. 大数据技术 - 为什么是SQL

    在大数据处理以及分析中 SQL 的普及率非常高,几乎是每一个大数据工程师必须掌握的语言,甚至非数据处理岗位的人也在学习使用 SQL.今天这篇文章就聊聊 SQL 在数据分析中作用以及掌握 SQL 的必要 ...

  7. c#高效准确的条形码、线性条码、QR二维码读写类库-SharpBarcode介绍

    SharpBarcode是一款支持.NET(C#,VB)的高效易用的条形码.QR二维码的读取和生成类库. 主要功能: 1.支持几乎所有常见类型的线性条形码和QR二维码的读取,高效读取,准确率高. 2. ...

  8. java 泛型和object比较

    引言 我们使用object和泛型做形参,都是为了让这个方法能接收更多类型的对象,让程序变得更健壮,代码复用率更高.当我们回看自己写的代码时会发现,好像使用泛型的地方使用object也可以,使用obje ...

  9. MySQL之Python与Mysql交互

    一:Python操作MySQL步骤 1:Python中操作MySQL的步骤 2.引入模块 在.py文件中引入pymysql模块 import pymysql pymysql是python的一个第三方与 ...

  10. 2019-07-31 Jquery

    Jquery是什么? jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是“ ...