zoukankan      html  css  js  c++  java
  • The Secret Life of Types in Swift

    An app which uses a results controller, which uses an ORM, which uses SQLite

    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.

    An app which uses a results controller, which uses an ORM, which uses SQLite

    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.

    An app which uses a results controller, which uses an ORM, which uses SQLite

    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

    An app which uses a results controller, which uses an ORM, which uses SQLite

    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

    An app which uses a results controller, which uses an ORM, which uses SQLite

    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

  • 相关阅读:
    在WINDOWS任务计划程序下执行PHP文件 PHP定时功能的实现
    使用Sublime Text 3进行Markdown 编辑+实时预览
    ni_set()函数的使用 以及 post_max_size,upload_max_filesize的修改方法
    CORS跨域的概念与TP5的解决方案
    tp5模型笔记---多对多
    微信小程序 GMT+0800 (中国标准时间) WXSS 文件编译错误
    ESP8266 LUA脚本语言开发: 外设篇-GPIO输入检测
    ESP8266 LUA脚本语言开发: 外设篇-GPIO输出高低电平
    ESP8266 LUA脚本语言开发: 准备工作-LUA文件加载与变量调用
    ESP8266 LUA脚本语言开发: 准备工作-LUA开发说明
  • 原文地址:https://www.cnblogs.com/feng9exe/p/10974815.html
Copyright © 2011-2022 走看看