# InlineArray conformance to common protocols

**URL:** <https://forums.swift.org/t/inlinearray-conformance-to-common-protocols/81585>\
**Category:** Discussion\
**Tags:** array\
**Created:** [August 11, 2025, 11:31am UTC](https://forums.swift.org/t/inlinearray-conformance-to-common-protocols/81585 "2025-08-11T11:31:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [August 11, 2025, 11:31am UTC](https://forums.swift.org/t/inlinearray-conformance-to-common-protocols/81585/1 "2025-08-11T11:31:40Z")

</div>

Are there plans to make `InlineArray` conforming to standard protocols?

```swift
'[3 of Int]' does not conform to protocol 'Equatable'
'[3 of Int]' does not conform to 'Encodable'
'[3 of Int]' does not conform to 'Decodable'

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 11, 2025, 2:38pm UTC](https://forums.swift.org/t/inlinearray-conformance-to-common-protocols/81585/2 "2025-08-11T14:38:00Z")

</div>

See the [relevant proposal text](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0453-vector.md#equatable-hashable-customstringconvertible-and-other-protocols):

> [@](#):
>
> There are a wide class of protocols that this type has the ability to conform to, but the issue is that it can only conform to them when the element conforms to them (this is untrue for `CustomStringConvertible`, but it still requires copyability). We could introduce these conformances but have them be conditional right now and generalize it later when we generalize these protocols, but if we were to ship say Swift X.Y with:
> 
> ```
> @available(SwiftStdlib X.Y) extension InlineArray: Equatable where Element: Equatable // & Element: Copyable
> 
> ```
> 
> and later down the road in Swift X.(Y + 1):
> 
> ```
> @available(SwiftStdlib X.Y) extension InlineArray: Equatable where Element: ~Copyable & Equatable
> 
> ```
> 
> Suddenly, this availability isn't quite right because the conformance that shipped in Swift X.Y doesn't support noncopyable elements. To prevent the headache of this and any potential new availability feature, we're holding off on these conformances until they are fully generalized.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [August 12, 2025, 12:53pm UTC](https://forums.swift.org/t/inlinearray-conformance-to-common-protocols/81585/3 "2025-08-12T12:53:18Z")

</div>

Thank you, good to know.

Until it is properly implemented I guess I could roll my own conformances, like these:

```swift
import Foundation

extension InlineArray: @retroactive Equatable where Element: Equatable {
    public static func == (lhs: Self, rhs: Self) -> Bool {
        !(0 ..< Self.count).contains { lhs[$0] != rhs[$0] }
    }
}

extension InlineArray: @retroactive Hashable where Element: Hashable {
    public func hash(into hasher: inout Hasher) {
        (0 ..< Self.count).forEach {
            hasher.combine(self[$0])
        }
    }
}

extension InlineArray: @retroactive Encodable where Element: Encodable {
    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        let array = (0 ..< Self.count).map { self[$0] }
        try container.encode(array)
    }
}

extension InlineArray: @retroactive Decodable where Element: Decodable {
    enum InlineArrayError: Error { case sizeMismatch(required: Int, provided: Int) }

    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let array = try container.decode([Element].self)
        if array.count != Self.count { throw InlineArrayError.sizeMismatch(required: Self.count, provided: array.count) }
        self = .init { array[$0] }
    }
}

extension InlineArray: @retroactive CustomStringConvertible {
    public var description: String {
        "\((0 ..< Self.count).map { self[$0] })"
    }
}

struct Test: Hashable, Codable {
    let x: [3 of Int]
}

func main() {
    let test = Test(x: [1, 2, 3])
    let data = try! JSONEncoder().encode(test)
    let s = String(data: data, encoding: .utf8)!
    print(s) // {"x":[1,2,3]}
    let test2 = try! JSONDecoder().decode(Test.self, from: data)
    print(test == test2) // true
    print(test) // Test(x: [1, 2, 3])
    print(test.hashValue) // -754052155688762463
    let s2 = #"{"x":[1,2,3, 4]}"#
    let data2 = s2.data(using: .utf8)!
    do {
        let test3 = try JSONDecoder().decode(Test.self, from: data2)
        print(test3)
    } catch {
        print(error) // sizeMismatch(required: 3, provided: 4)
    }

}
main()

```
