# roles

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

{% hint style="success" %}
程式開發中的角色。\
從<mark style="color:yellow;">開發程式</mark>到使用應用程式之間所牽涉到的人 (或<mark style="color:yellow;">程式碼</mark>)。\
:u6307: 通常指「某一段<mark style="color:yellow;">程式碼</mark>」
{% endhint %}

<table><thead><tr><th width="258">角色</th><th>行為</th></tr></thead><tbody><tr><td>實現者╱<a href="roles/implementer">implementer</a></td><td>寫<a href="../../swift/type/category/basic/func">函數</a>、<a href="../../swift/type">型別</a>定義的人 (或程式碼)。</td></tr><tr><td>使用者╱<a href="roles/user">user</a></td><td>使用<a href="../../swift/type/category/basic/func">函數</a>、<a href="../../swift/type">型別</a>定義的人 (或程式碼)。</td></tr><tr><td>終端使用者╱<mark style="color:orange;">end user</mark></td><td>使用應用程式的人 (不涉及程式開發)。</td></tr></tbody></table>

<table data-header-hidden><thead><tr><th width="150">特性</th><th>some</th><th>any</th><th>generic</th></tr></thead><tbody><tr><td></td><td><a href="../../swift/type/category/some-any-generics/some"><code>some</code></a></td><td><a href="../../swift/type/category/some-any-generics/any"><code>any</code></a></td><td><a href="../../swift/type/category/generic-types">generic</a></td></tr><tr><td>範例</td><td><pre class="language-swift"><code class="lang-swift">// implementer
func f() -> some Shape {
    Circle()
}

// user
let a = f() </code></pre></td><td><pre class="language-swift"><code class="lang-swift">// implementer
func g() -> any Shape {
// omitted ...
}

// user
let b = g() </code></pre></td><td><pre class="language-swift"><code class="lang-swift">// implementer
struct Container\<T> {
var value: T
}

// user
let c = Container(value: 42) </code></pre></td></tr><tr><td>型別決定者</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f477">👷</span> 實現者</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f9d1-1f4bc">🧑‍💼</span> 使用者</td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f9d1-1f4bc">🧑‍💼</span> 使用者</td></tr><tr><td>型別決定期</td><td><span data-gb-custom-inline data-tag="emoji" data-code="2699">⚙️</span> <mark style="color:green;">編譯期</mark></td><td><span data-gb-custom-inline data-tag="emoji" data-code="1f3ce">🏎️</span> <mark style="color:red;">執行期</mark></td><td><span data-gb-custom-inline data-tag="emoji" data-code="2699">⚙️</span> <mark style="color:green;">編譯期</mark></td></tr><tr><td>允許多型別</td><td><p><mark style="color:red;">否</mark>，<mark style="color:orange;">單型別</mark> (homogeneous type)</p><p></p><p><mark style="color:yellow;">對使用者隱藏具體型別</mark>，故稱為 <a href="../../swift/type/category/some-any-generics/some">opaque type</a>。</p></td><td><p><mark style="color:green;">是</mark>，多型別 </p><p>(heterogeneous type)</p><p></p><p><mark style="color:yellow;">將具體型別封裝起來</mark>的型別，故稱為 <a href="../../swift/type/category/some-any-generics/any">boxed protocol type</a>。</p></td><td><p><mark style="color:orange;">介於是與否之間</mark> </p><p></p><p>使用者可指定不同的 <mark style="color:blue;"><code>T</code></mark>，因此<mark style="color:green;">可產生許多不同的具體型別</mark>。</p><p></p><p>不過一旦指定了 <mark style="color:blue;"><code>T</code></mark> 之後，此<a href="../../swift/type/category/generic-types">泛型</a><mark style="color:yellow;">馬上變為單型別的具體型別</mark>。</p></td></tr></tbody></table>

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

<details>

<summary><span data-gb-custom-inline data-tag="emoji" data-code="1f477">👷</span> 實現者 (implementer)</summary>

<pre class="language-swift"><code class="lang-swift">// custom protocol
<strong>protocol MyShape {
</strong>    func area() -> Double
}

// custom types conforming to MyShape
struct MyCircle: MyShape {
    var radius: Double
    func area() -> Double { .pi * radius * radius }
}

struct MySquare: MyShape {
    var size: Double
    func area() -> Double { size * size }
}

// return type: opaque type (some)
func makeCircle() -> some MyShape {  // ⭐️ 返回值具體型別：`MyCircle`
    return MyCircle(radius: 10)      // ⭐️ 決定者：實現者
}                                    // ⭐️ 決定期：編譯期 (compile time) 

// return type: boxed protocol type (any)
func makeAnyShape() -> any MyShape { // ⭐️ 返回值具體型別：(動態決定)
    let n = Int.random(in: 1...6)    // ⭐️ 決定者：使用者
    return (n % 2 == 0)              // ⭐️ 決定期：執行期 (run time)
        ? MyCircle(radius: 10)      
        : MySquare(size: 4)
}                                     

// generic type
struct Container&#x3C;T> {
    var value: T
}
</code></pre>

</details>

<details>

<summary><span data-gb-custom-inline data-tag="emoji" data-code="1f9d1-1f4bc">🧑‍💼</span> 使用者 (user)</summary>

```swift
let shape = makeCircle()        // ⭐️ 使用者：只知道返回值是 `some MyShape`
print(shape.area())             // ⭐️ 使用者：可使用 `MyShape` 的方法

let a = Container(value: 42)    // ⭐️ 泛型的具體型別：`Container<Int>`
                                // ⭐️ 決定者：使用者
                                // ⭐️ 決定期：編譯期 (compile time)
```

</details>
{% 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 %}
