<div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><span style="font-size:12.8px">&gt; </span><span style="font-size:12.8px">automatically cast rawValue when assigning an enum value to</span></div><span style="font-size:12.8px">a variable or argument of the type of the enum&#39;s raw value</span><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">That&#39;s exactly what I&#39;m thinking, Leonardo. I was a little bit trying to avoid specifics because I don&#39;t understand the differences between autocasting and inference.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">There&#39;s a small miscommunication though--This api works great since it accepts the enum type, rather than the underlying value type. Signature:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">static func fadeIn(view: UIView?, withSpeed: Animate.transitionSpeed = Animate.transitionSpeed.fast)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The goal was to provide my users with a good default and an easy set of configurable options at the call site like `.fast`, `.normal`, `.slow`.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">(unrelated: +1 on inferring type of function params with default values, I hit this all the time)</span><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Calling:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Animate.fadeIn(myView, withSpeed: .slow)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Works great.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">This library of methods is a facade for CABasicAnimation or UIView animations which take an NSTimeInterval, and that&#39;s where the &quot;autocasting&quot; would come into play.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br><div><span style="font-size:12.8px"><br></span></div>------------------------------</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Message: 31</span><br style="font-size:12.8px"><span style="font-size:12.8px">Date: Fri, 13 May 2016 16:58:06 -0300</span><br style="font-size:12.8px"><span style="font-size:12.8px">From: Leonardo Pessoa &lt;</span><a href="mailto:me@lmpessoa.com" style="font-size:12.8px" target="_blank">me@lmpessoa.com</a><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">To: Swift-evolution &lt;</span><a href="mailto:swift-evolution@swift.org" style="font-size:12.8px" target="_blank">swift-evolution@swift.org</a><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">Subject: Re: [swift-evolution] Could enums have their rawValue type</span><br style="font-size:12.8px"><span style="font-size:12.8px">        inferred?</span><br style="font-size:12.8px"><span style="font-size:12.8px">Message-ID:</span><br style="font-size:12.8px"><span style="font-size:12.8px">        &lt;</span><a href="mailto:CANTOS57wksMRzUAMq0ncf1QAd82vhudf21tnea6wHjhBOFr1PA@mail.gmail.com" style="font-size:12.8px" target="_blank">CANTOS57wksMRzUAMq0ncf1QAd82vhudf21tnea6wHjhBOFr1PA@mail.gmail.com</a><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">Content-Type: text/plain; charset=&quot;utf-8&quot;</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Eric, I think I understood your proposal. If I may explain in other words</span><br style="font-size:12.8px"><span style="font-size:12.8px">it would be &quot;to automatically cast rawValue when assigning an enum value to</span><br style="font-size:12.8px"><span style="font-size:12.8px">a variable or argument of the type of the enum&#39;s raw value&quot;, am I right? I</span><br style="font-size:12.8px"><span style="font-size:12.8px">think this would imply a little more inference and type checking rules from</span><br style="font-size:12.8px"><span style="font-size:12.8px">the compiler and maybe even take a little longer to fully compile code. I&#39;m</span><br style="font-size:12.8px"><span style="font-size:12.8px">not sure it&#39;s feasible but from your examples, I can see how it enhances</span><br style="font-size:12.8px"><span style="font-size:12.8px">readability of the code, so I&#39;m +1 for it. My only concern is that you</span><br style="font-size:12.8px"><span style="font-size:12.8px">would still need to fully declare the enum&#39;s name where the value of the</span><br style="font-size:12.8px"><span style="font-size:12.8px">enum is used. Taking from your own example</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">    Animate.fadeIn(view, withSpeed: .fast)</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">couldn&#39;t be called that way if withSpeed expects and NSTimeInterval because</span><br style="font-size:12.8px"><span style="font-size:12.8px">the compiler won&#39;t know whether you&#39;re refering to transitionSpeed or to</span><br style="font-size:12.8px"><span style="font-size:12.8px">ambientAnimationSpeed. You would still have to call it like</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">    Animate.fadeIn(view, withSpeed: transitionSpeed.fast)</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">even if you had only one possible enum value over all declared enums</span><br style="font-size:12.8px"><span style="font-size:12.8px">because that would still force the compiler to search for each value over</span><br style="font-size:12.8px"><span style="font-size:12.8px">all known enums to define where the value you&#39;re using comes from and make</span><br style="font-size:12.8px"><span style="font-size:12.8px">sure there are no two enums with the same value.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Aside from that, I good with the idea.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">On 13 May 2016 at 15:09, Eric Miller via swift-evolution &lt;</span><br style="font-size:12.8px"><a href="mailto:swift-evolution@swift.org" style="font-size:12.8px" target="_blank">swift-evolution@swift.org</a><span style="font-size:12.8px">&gt; wrote:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; This might open a larger can of worms than I imagine, but what do you</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; folks think about using the `rawValue` of an enum when that rawValue is a</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; fit for the expected type?</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; Use case.</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; I&#39;m making an animation facade, and it has some speed presets:</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; class Animate {</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   enum transitionSpeed: NSTimeInterval {</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;     case fast = 0.15</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;     case slow = 0.5</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   }</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   enum ambientAnimationSpeed: NSTimeInterval {</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;     case fast = 1.0</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;     case slow = 5.0</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   }</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   ...</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; }</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; I did them with static variables at first but that made the call site</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; verbose. Compare:</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; Animate.fadeIn(view, withSpeed: Animate.cfg.transitionFast)</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; Animate.fadeIn(view, withSpeed: .fast)</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; So, I like the enum approach better, but my library code has to use</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; `rawValue` to do anything with the actual value, of course:</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; static func fadeIn(view: UIView?, withSpeed duration:transitionSpeed =</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; .fast) {</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   ...</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;   UIView.animateWithDuration(</span><span style="font-size:12.8px">duration.rawValue, animations: { })</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; }</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; It&#39;s not a serious issue, but the code is more clear and beautiful if it</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; has just myIntent, rather than myIntent.rawValue.</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; I&#39;ve hit this issue when modeling other things, such as:</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; * server fault codes</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; * HTTP status codes</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; * Currency codes</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; * Days of the week</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; Would it be appropriate to &quot;autocast&quot; to the rawValue of the enum when the</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; rawValue&#39;s Type matches the type expectation of the API? Or would this</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; introduce a bunch of type system uncertainty?</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt;</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; Maybe this could be implemented as a protocol? It feels almost like the</span><br style="font-size:12.8px"><span style="font-size:12.8px">&gt; convenience of `CustomStringConvertible`&#39;s `description` property.</span></div></div></div></div>