# implementer

[dev](https://lochiwei.gitbook.io/ios/master) ⟩ [terms](https://lochiwei.gitbook.io/ios/master/term) ⟩ [roles](https://lochiwei.gitbook.io/ios/master/term/roles) ⟩ implementer&#x20;

{% hint style="success" %} <mark style="color:purple;">實現者</mark>╱*<mark style="color:purple;">implementer</mark>*╱*<mark style="color:purple;">provider</mark>*\
程式開發中，寫[函數](https://lochiwei.gitbook.io/ios/swift/type/category/basic/func)、[型別](https://lochiwei.gitbook.io/ios/swift/type)定義的人 (或程式碼)。\
:u6307: 通常指「某一段<mark style="color:yellow;">程式碼</mark>」
{% endhint %}

{% tabs %}
{% tab title="💈 範例" %}

```swift
// ⭐️ implementer: 
// 在這裡，`makeCircle` 就是實現者，他決定了返回值的具體型別是 `Circle`，
// 雖然對外只暴露 `some Shape`(使用者只知道返回值是 `some Shape`)。
func makeCircle(radius: Double) -> some Shape {
    return Circle(radius: radius)
}

// ⭐️ user:
// 呼叫 `makeCircle` 的程式碼就是使用者，使用者只知道返回型別符合 Shape 協議，
// 但不知道具體型別是什麼（因為返回值是 some Shape）。
let shape = makeCircle(radius: 5)
```

<pre class="language-swift"><code class="lang-swift">// ⭐️ implementer: 定義型別、函數
// ------------------------------
<strong>protocol MyShape {
</strong>    func area() -> Double
}

struct MyCircle: MyShape {
    var radius: Double
    func area() -> Double { .pi * radius * radius }
}

func makeCircle() -> some MyShape {
    return MyCircle(radius: 10)      // ⭐️ 實現者決定具體型別：`MyCircle`
}

// ⭐️ user: 使用型別、函數
// ------------------------------
let shape = makeCircle()             // ⭐️ 使用者只能知道它是：`some MyShape`
print(shape.area())                  // ⭐️ 可使用 `MyShape` 協議的方法
</code></pre>

{% endtab %}

{% tab title="👥 相關" %}

* :scales: [some-any-generics](https://lochiwei.gitbook.io/ios/swift/type/category/some-any-generics "mention")：實現者與使用者對型別有不同的控制權。
  {% endtab %}

{% tab title="📗 參考" %}

* ChatGPT ⟩ [學習 Swift](https://chatgpt.com/share/675507f6-f118-800e-bbf4-5ca6d40ad646)&#x20;
  {% endtab %}
  {% endtabs %}
