Swift Mental Models

4 阅读3分钟

Swift Mental Models

Swift 的核心心智模型(Mental Models)地图。

这个文件夹不是 Swift API 手册,也不是完整教程。

它的作用是建立一套稳定的认知骨架,帮助把以后遇到的 Swift 知识放到正确的位置。


1. Core Idea

学习 Swift 时,不要只记:

语法
API
代码写法

更重要的是理解:

它是什么?
属于哪个概念?
和什么概念有关?
解决什么问题?

目标不是建立越来越多的模型,而是:

用少量、可复用的模型解释越来越多的知识。


2. Core Structure

Swift 中很多概念可以沿着这条主线理解:

Type
  ↓
Value / Reference Semantics
  ↓
Instance & Lifetime
  ↓
Member
  ↓
Access / Dispatch
  ↓
Memory

例如:

Person
  ↓
Type
  ↓
person
  ↓
Instance
  ↓
name / hello()
  ↓
Member
  ↓
person.name
  ↓
Member Access

其他重要模型会与这条主线连接:

                    Type System
                         │
          ┌──────────────┼──────────────┐
          ↓              ↓              ↓
   Value / Reference   Protocol      Generic
      Semantics         Model         Model
          │
          ↓
 Instance & Lifetime
          │
          ↓
      Members
          │
          ↓
 Access & Dispatch
          │
          ↓
       Memory

Function & Closure ─────┬──→ Lifetime
                        └──→ Collection

Collection ───────────────→ Function & Closure

Error & Optional ─────────→ Type System

Concurrency ──────────────→ Type + Lifetime + Memory + Protocol

3. Models

#ModelCore Question
01Type SystemWhat kind of thing is this?
02Value & Reference SemanticsWhat does copying mean?
03Instance & LifetimeWhat is an instance, and how long does it exist?
04Member SystemWhat belongs to a Type?
05Function & Closure ModelHow is behavior represented and passed around?
06Access & Dispatch ModelHow is a member found and which implementation runs?
07Memory ModelHow are values and references represented and stored?
08Protocol ModelHow are capabilities and requirements expressed?
09Generic ModelHow can code abstract over Types?
10Collection ModelHow are groups of values represented and operated on?
11Error & Optional ModelHow are absence and failure represented?
12Concurrency ModelHow is concurrent work structured safely?

4. How to Use

以后遇到新知识,先问:

① What is it?(它是什么?)
↓
② Which model does it belong to?(属于哪个 Mental Model?)
↓
③ What existing concept is it related to?(和已有的什么概念有关?)
↓
④ What problem does it solve?(它解决什么问题?)

优先挂到已有模型

不要因为出现一个新名词,就建立一个新的 Mental Model。

例如:

Computed Property
      ↓
Member System
      ↓
Property
      ↓
Memory Model

Computed Property 不需要成为新的 Mental Model。

再比如:

static property
      ↓
Member System
      ↓
Type Member

5. 不要混淆不同层次

尤其要区分:

Language-level

Type
Member
Property
Method
Protocol
Generic

Implementation-level

Storage
Memory
Reference Counting
Dispatch
Metadata

例如:

Member ≠ Memory
Method ≠ Instance 中的一块内存
struct ≠ 一定在 Stack
class ≠ 一定在 Heap

这些是不同层次的问题。


6. Syntax ≠ Concept

语法只是概念的一种表达方式。

例如:

person.name

更重要的是:

Type
 ↓
Member
 ↓
Member Access

再例如:

person.sayHello()

可以拆成:

Member
 ↓
Member Access
 ↓
Method
 ↓
Call
 ↓
Dispatch / Execution

7. Mental Model vs Discovery Notes

这里保存的是:

已经整理、相对稳定的正确模型。

而学习过程中出现的:

原来的理解
 ↓
遇到问题
 ↓
产生疑问
 ↓
发现错误
 ↓
修正

可以记录在 Discovery / Exploration 类笔记中。

也就是说:

Discovery Notes
    ↓
探索过程、疑问、错误认知
    ↓
最终修正
    ↓
Mental Models
    ↓
稳定的知识结构

这样既保留你的思考过程,又不会让正式知识文档变得混乱。


8. A Simple Test

以后遇到新概念,可以尝试把它放进已有模型。

例如:

static

Type
 ↓
Member
 ↓
Type Member

Computed Property

Member
 ↓
Property
 ↓
Computed Property
 ↓
Memory

Closure Capture

Function & Closure
 ↓
Capture
 ↓
Lifetime
 ↓
Memory

Protocol Dispatch

Protocol
 ↓
Member Requirement
 ↓
Conformance
 ↓
Dispatch

如果能够这样解释,通常说明:

它是已有模型中的一个新知识,而不是一个全新的模型。


9. Guiding Principle

不要把知识收集成孤立的知识点。

建立少量稳定的 Mental Models,让新的知识不断挂载进去。

目标不是知道越来越多的模型,而是用越来越少的模型解释越来越多的东西。