跳过正文
  1. Posts/

Codable && Tuple

·2 分钟·
目录

During my learning of Swift, many interesting things I will find. Codable is one of them.

Today, I defined a model with a tuple type, then Xcode told me some error. Codes may like as below.

Codable Person

You may want Xcode automatically complete all the codable stuff. However, life is hard. Codes like these can even not be compiled. Xcode will tell you name cannot be synthesize the Person because of the FullName.

error: default.playground:5:8: error: type 'Person' does not conform to protocol 'Decodable'
struct Person: Codable {
       ^

Swift.Decodable:2:12: note: protocol requires initializer 'init(from:)' with type 'Decodable'
    public init(from decoder: Decoder) throws
           ^

default.playground:6:9: note: cannot automatically synthesize 'Decodable' because 'FullName' (aka '(firstName: String, secondName: String)') does not conform to 'Decodable'
    var name: FullName
        ^

error: default.playground:5:8: error: type 'Person' does not conform to protocol 'Encodable'
struct Person: Codable {
       ^

Swift.Encodable:2:17: note: protocol requires function 'encode(to:)' with type 'Encodable'
    public func encode(to encoder: Encoder) throws
                ^

default.playground:6:9: note: cannot automatically synthesize 'Encodable' because 'FullName' (aka '(firstName: String, secondName: String)') does not conform to 'Encodable'
    var name: FullName
        ^

So, We know that if a struct or class is codable implicitly, it must not contain properties which are not codable. Tuple is one of them.

Some people argue that why tuple cannot be designed as a codable type? Yeah, the hope is that tuples could conform to protocols in future. This is covered in the generics manifesto as “Extensions of Structural Types 28”.

Extensions of structural types Currently, only nominal types (classes, structs, enums, protocols) can be extended. One could imagine extending structural types–particularly tuple types–to allow them to, e.g., conform to protocols. For example, pulling together variadic generics, parameterized extensions, and conditional conformances, one could express “a tuple type is Equatable if all of its element types are Equatable”:

extension<...Elements : Equatable> (Elements...) : Equatable {   // extending the tuple type "(Elements...)" to be Equatable
}

There are some natural bounds here: one would need to have actual structural types. One would not be able to extend every type:

extension<T> T { // error: neither a structural nor a nominal type
}

And before you think you’re cleverly making it possible to have a conditional conformance that makes every type T that conforms to protocol P also conform to protocol Q, see the section “Conditional conformances via protocol extensions”, below:

extension<T : P> T : Q { // error: neither a structural nor a nominal type
}

So for now, you have to synthesize the tuple type by yourself.

Synthesize Properties by youself

References
#

  1. Codable Tuples
  2. Swift GenericsManifesto.md

相关文章

一道 Swift Quiz

·1 分钟
这两天在 Twitter 上看到一道题目,主要是考察 overload 和 type(of:) 的知识点,本文仅做记录,关于 MetaType 会单独写一篇文章来总结。

@autoclosure && @escape

·3 分钟
我们知道在 swift 中,闭包(closure)是一等公民,因此可以被当作参数传递,在学习 swift 的过程中经常会看到某些关键字修饰该闭包,@autoclosure, @escape 就是其中比较常见的两种关键字。

关于 Library 和 Framework

·8 分钟
Library 和 Framework 的概念大家应该脑海里都有一些,本文旨在讲述下基本概念,没有对每个字节都了如指掌。关于基本的编译过程在 Build Process 一文中也大概讲述了一些。

Build Process

编程语言的处理过程大致会有五个阶段,其每个阶段均有对应的工具: 预处理器 Preprocessor 编译器 Compiler 汇编器 Assembler 链接器 Linker 加载器 Loader 我们以一个简单的源文件,来看看具体这几个步骤都做了哪些事情。