Type to hold any enum

I had a need to hold a reference to any string enum, no matter where it was declared.

Here is my solution :

public protocol StringEnumType : Hashable & RawRepresentable where Self.RawValue == String { }

This is then attached to an enum. e.g. :

public enum CellIdentifier : String, StringEnumType
{
  case value
  case summary
}

… or you can add it in an extension :

extension CellIdentifier : StringEnumType { }

Of course, due to PAT restrictions, this has to be used to qualify a generic parameter type but, it does then allow me to declare a method (or other generic type) that will work with a string-based enum, without knowing what the concrete enum type will be.

Now, tell me that "everybody" already knows this :roll_eyes::laughing:

If you just want a convenient way to define a set of constraints on a generic placeholder, another option is a generic typealias, e.g:

public enum CellIdentifier : String {
  case value
  case summary
}

typealias StringRepresentable<T : RawRepresentable & Hashable> = T
  where T.RawValue == String

func foo<T>(_ x: StringRepresentable<T>) {
  print(x.rawValue)
}

foo(CellIdentifier.summary) // summary

Unfortunately, life is not that simple. This is framework code and nothing is fixed until the app that uses the framework firms up what types are actually going to be used. There is a set of inter-related generic types in the framework, all of which are bound to specific types in the app, so mistakes at the app level are hard to make.