title: Converting Swift enum to Class
body: <p>I have the following code to implement animation models based on <a href="https://stackoverflow.com/questions/69036422/swift-enum-associated-value-with-different-types">this</a> answer:</p>
  <pre><code> public enum AnimationType {
     public enum Kind&lt;Value&gt; {
        case scalar(Value)
        case keyframes([Keyframe&lt;Value&gt;])
     }
  
      case position(Kind&lt;Float&gt;)
      case scale(Kind&lt;Float&gt;)
      case rect(Kind&lt;CGRect&gt;)
      case transform(Kind&lt;CGAffineTransform&gt;)
      ....
      ....
    }
  
    public class Keyframe&lt;T&gt; : Codable, Comparable where T:Codable, T:Interpolatable  {
        public var time:CMTime
        public var property:String
        public var value:T
        ...
        ...
    }
  </code></pre>
  <p>This data model was chosen as I found it type safe for each animation property as it couples data type with the property (for instance, value for property transform can only be CGAffineTransform, the code wouldn't accept anything else). But now I face two troubles:</p>
  <ol>
  <li><p>Directly fetching property value or modifying keyframes is not so easy, one needs to write a big switch statement everytime or atleast  <em>if case let</em> statement to fetch the property, which makes code look messy if done at hundreds of places,</p>
  </li>
  <li><p>Most important, Swift enums are pass by value but I realize I need pass by reference or class based implementation. This is because it would be much easier to modify the underlying object parameters in case of pass by reference. In case of pass by value such as enum, I need to again create new values and pass them to the animation code (which has it's own data model for rendering animation). Reconstructing or updating data structures for animation rendering is a pain and can be avoided with class.</p>
  </li>
  </ol>
  <p>However, I am not sure if there is such a type safe approach to convert such an enum to class, or make enum pass by reference for that matter. Any inputs are welcome.</p>