# Type to hold any enum

**URL:** <https://forums.swift.org/t/type-to-hold-any-enum/24633>\
**Category:** Using Swift\
**Created:** [May 17, 2019, 4:26pm UTC](https://forums.swift.org/t/type-to-hold-any-enum/24633 "2019-05-17T16:26:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joanna\_Carter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joanna_carter/32/1581_2.png) [@Joanna\_Carter](https://forums.swift.org/u/Joanna_Carter)\
**Post date:** [May 17, 2019, 4:26pm UTC](https://forums.swift.org/t/type-to-hold-any-enum/24633/1 "2019-05-17T16:26:27Z")

</div>

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

Here is my solution :

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

```

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

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

```

… or you can add it in an extension :

```swift
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 🙄😆

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [May 17, 2019, 5:31pm UTC](https://forums.swift.org/t/type-to-hold-any-enum/24633/2 "2019-05-17T17:31:12Z")

</div>

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:

```swift
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

```

---

<div class="post-metadata">

**Author:** ![Joanna\_Carter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joanna_carter/32/1581_2.png) [@Joanna\_Carter](https://forums.swift.org/u/Joanna_Carter)\
**Post date:** [May 17, 2019, 5:49pm UTC](https://forums.swift.org/t/type-to-hold-any-enum/24633/3 "2019-05-17T17:49:50Z")

</div>

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.
