> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/debugging/nonnominaltypewrapper.md).

# NonNominalTypeWrapper

{% tabs %}
{% tab title="👔 NonNominalTypeWrapper" %}
💾 程式：[paiza.io](https://paiza.io/projects/BgeXldbalSZcvt3C_5qcQg)    ⬆️ 需要： [HasMirrors](/ios/swift/debugging/hasmirrors.md)

```swift
// --------------------------------
//     ⭐ NonNominalTypeWrapper
// --------------------------------

/// non-nominal type: 
/// ⛔ - can't be extended. 
/// ⛔ - can't conform to protocols.
/// use this type to wrap non-nominal types, so they can do these things.
public struct NonNominalTypeWrapper<T> {
    let subject: T     // T is non-nominal (i.e. Any, Tuple)
}

/// convenience init
/// usage: `NonNominalTypeWrapper(instance)`
extension NonNominalTypeWrapper {
    // ⭐ 注意：這裡不能寫成 init<T>
    // 不然會引進「新的型別參數 `T`」（是的，跟「舊的型別參數 `T`」
    // 名字一模一樣），然後產生「令人傻眼的錯誤訊息」：
    // 「⛔ cannot assign value of type 'T' to type 'T'」
    public init(_ subject: T){
        self.subject = subject
    }
}

/// ⭐ custom extension (Loggable conformance)
extension NonNominalTypeWrapper: Loggable {

    @discardableResult
    public func log() -> Self { 
        print(String(describing: self.subject)) 
        return self 
    }
    
    @discardableResult
    public func reflect() -> Self { 
        print(String(reflecting: self.subject))
        return self 
    }
    
    // mirror (object tree)
    @discardableResult
    public func mirror() -> Self {
        mirrorChildren(of: self.subject)
    }
}
```

{% endtab %}

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

```swift
// ----------------------------
//     ⭐ non-nominal types
// ----------------------------

// tuple
NonNominalTypeWrapper((true, 32))
    .log()                      // (true, 32)
    .reflect()                  // (true, 32)
    .mirror()
    // ------------------------------
    //     children of (true, 32)    
    // ------------------------------
    //   • .0: true (Bool)
    //   • .1: 32 (Int)
    // ------------------------------

NonNominalTypeWrapper((first: "Taylor", last: "Swift"))
    .log()              // (first: "Taylor", last: "Swift")
    .reflect()          // (first: "Taylor", last: "Swift")
    .mirror()
    // ----------------------------------------------------
    //     children of (first: "Taylor", last: "Swift")    
    // ----------------------------------------------------
    //   • first: Taylor (String)
    //   • last: Swift (String)
    // ----------------------------------------------------

NonNominalTypeWrapper((name: "Tony", "Stark"))
    .log()              // (name: "Tony", "Stark")
    .reflect()          // (name: "Tony", "Stark")
    .mirror()
    // -------------------------------------------
    //     children of (name: "Tony", "Stark")    
    // -------------------------------------------
    //   • name: Tony (String)
    //   • .1: Stark (String)
    // -------------------------------------------

// closure
let closure = { (a: Int) -> Int in a * 2 }

NonNominalTypeWrapper(closure)
    .log()              // (Function)
    .reflect()          // (Function)
    .mirror()           // no children
```

{% endtab %}

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

* [non-nominal types](/ios/swift/type/category/non-nominal-types.md)
* [Mirror](/ios/swift/debugging/mirror.md)
* [HasMirrors](/ios/swift/debugging/hasmirrors.md) - required protocol.
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swift/debugging/nonnominaltypewrapper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
