SwiftGraph icon indicating copy to clipboard operation
SwiftGraph copied to clipboard

Make `Graph` conform to `Codable` only when the vertices are `Codable`?

Open RJPoelstra opened this issue 1 year ago • 2 comments

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.

RJPoelstra avatar Jan 24 '24 10:01 RJPoelstra

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 Codable conformance to an existing data type in an extension that just ignores the values that are not possible to make Codable

davecom avatar Jan 25 '24 03:01 davecom

Thanks for the suggestions. I'll look into them.

RJPoelstra avatar Jan 26 '24 07:01 RJPoelstra