SwiftGraph
SwiftGraph copied to clipboard
Make `Graph` conform to `Codable` only when the vertices are `Codable`?
Hi,
Native swift types like Array only conform to Codable when their elements are Codable.
It looks like Graph doesn't have any special need for being Codable.
Would it be possible to make Graph conform to Codable only when its vertices are? I've a use case where I'm not interested in the Graph being codable and adding conformance to the vertex type would be a lot of work.
That's a good question. I think we already went through attempting to make Codable conditional in an earlier version of Swift but ran into some compiler limitations. I would guess this is possible now.
A couple alternatives you could do for now:
- Make a unique ID for each vertex that you use in SwiftGraph and lookup the ancillary data for your specific needs in a separate data structure like a simple Dictionary mapping IDs to data
- Add
Codableconformance to an existing data type in an extension that just ignores the values that are not possible to makeCodable
Thanks for the suggestions. I'll look into them.