# Generics with diffable data source for multiple sections / cell types

**URL:** <https://forums.swift.org/t/generics-with-diffable-data-source-for-multiple-sections-cell-types/81269>\
**Category:** Using Swift\
**Tags:** generics, protocols\
**Created:** [July 23, 2025, 9:03pm UTC](https://forums.swift.org/t/generics-with-diffable-data-source-for-multiple-sections-cell-types/81269 "2025-07-23T21:03:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gs1221xy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gs1221xy/32/42261_2.png) [@gs1221xy](https://forums.swift.org/u/gs1221xy)\
**Post date:** [July 23, 2025, 9:03pm UTC](https://forums.swift.org/t/generics-with-diffable-data-source-for-multiple-sections-cell-types/81269/1 "2025-07-23T21:03:38Z")

</div>

Hi guys.

I am trying to implement collection view screen with multiple sections and cell types using diffable data source. I define it using Sections enum and SectionItem struct which keeps nested model for cells configuration

```swift
private var dataSource: UICollectionViewDiffableDataSource<Behavior.Section, SectionItem>?

public typealias HashableAndSendable = Hashable & Sendable

public protocol ItemProtocol: HashableAndSendable {
  var model: any HashableAndSendable { get }
}

public struct SectionItem: ItemProtocol {

  public var model: any HashableAndSendable

  public func hash(into hasher: inout Hasher) {
    hasher.combine(model)
  }

  public static func == (lhs: SectionItem, rhs: SectionItem) -> Bool {
    lhs.model.hashValue == rhs.model.hashValue
  }
}

```

I want to have cells with configure function with its own model struct as a param

```swift
public protocol ConfigurableProtocol {
  associatedtype Model: HashableAndSendable
  @MainActor
  func configure(with model: Model)
}

public typealias ConfigurableCell = UICollectionViewCell & ConfigurableProtocol

```

In my View Controller I am setting up my cells using UICollectionView.CellRegistration handler

```swift
var cellsRegistrations = [String: Any]()

  func registerCell<Cell>(
    cellType: Cell.Type,
    handler: @escaping UICollectionView.CellRegistration<Cell, SectionItem>.Handler
  )
  where Cell: ConfigurableCell {
    cellsRegistrations[String(describing: cell.self)] = UICollectionView.CellRegistration<Cell, SectionItem>(handler: handler)
  }

func setupCells() {
    for type in behavior.cellsTypes() {
      registerCell(cellType: type) { [unowned self] cell, indexPath, itemIdentifier in
        configure(cell: cell, model: itemIdentifier.model)
      }
    }
  }

```

but when trying to call configure functions I have no way of casting to proper model type of a cell

```swift
func configure(
    cell: some ConfigurableCell,
    model: some HashableAndSendable
  ) {
    // error here: 'some' types are only permitted in properties, subscripts, and functions
    cell.configure(with: model as? (some ConfigurableCell).Model)   
 }

```

Can I define these relationships like that or not ? What can you recommend

Thanks a lot

---

<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:** [July 23, 2025, 11:38pm UTC](https://forums.swift.org/t/generics-with-diffable-data-source-for-multiple-sections-cell-types/81269/2 "2025-07-23T23:38:38Z")

</div>

I'd do it simpler, e.g.:

```swift
    @MainActor func configure(cell: UICollectionViewCell, model: any Model) {
        cell.configure(with: model)
    }

```

Note that this fragment is problematic:

```swift
public struct SectionItem: ItemProtocol {

  public var model: any HashableAndSendable

  public func hash(into hasher: inout Hasher) {
    hasher.combine(model)
  }

  public static func == (lhs: SectionItem, rhs: SectionItem) -> Bool {
    lhs.model.hashValue == rhs.model.hashValue
  }
}

```

1. you are comparing hashValues, this is suspicious (hashValues could be equal while the items themselves are not).
2. if you are comparing hashValues (which is wrong per (1) but still) – at least use that in your hash implementation. `hasher.combine(model)` is not the same as `hasher.combine(model.hashValue)`

This smells "speculative generality", and in cases like this I'd recommend to step back and "de-generalise" the code – make it as concrete as possible initially. Once it's working, and you feel making it more generic would be beneficial (e.g. because it will half the code size) - slowly introduce generalisations back, one step at a time.

---

<div class="post-metadata">

**Author:** ![gs1221xy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gs1221xy/32/42261_2.png) [@gs1221xy](https://forums.swift.org/u/gs1221xy)\
**Post date:** [July 24, 2025, 9:41am UTC](https://forums.swift.org/t/generics-with-diffable-data-source-for-multiple-sections-cell-types/81269/3 "2025-07-24T09:41:52Z")

</div>

Yes, you're right I could implement Hashable and Equatable better and I will.

When it comes to the **configure** function I don't like it accepting any model and to have to cast to a proper struct it uses for configuration. This is a bad design in my opinion. Doesn't swift support better implementation? I would be disappointed
