<div dir="ltr"><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif">Eric, I think I understood your proposal. If I may explain in other words it would be "to automatically cast rawValue when assigning an enum value to a variable or argument of the type of the enum's raw value", am I right? I think this would imply a little more inference and type checking rules from the compiler and maybe even take a little longer to fully compile code. I'm not sure it's feasible but from your examples, I can see how it enhances readability of the code, so I'm +1 for it. My only concern is that you would still need to fully declare the enum's name where the value of the enum is used. Taking from your own example</div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><br></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><span style="font-family:arial,sans-serif;font-size:12.8px"> Animate.fadeIn(view, withSpeed: .fast)</span><br></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><span style="font-family:arial,sans-serif;font-size:12.8px"><br></span></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><span style="font-family:arial,sans-serif;font-size:12.8px">couldn't be called that way if withSpeed expects and NSTimeInterval because the compiler won't know whether you're refering to </span><span style="font-size:12.8px;font-family:arial,sans-serif">transitionSpeed or to </span><span style="font-size:12.8px;font-family:arial,sans-serif">ambientAnimationSpeed</span><span style="font-family:arial,sans-serif;font-size:12.8px">. You would still have to call it like</span></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><span style="font-family:arial,sans-serif;font-size:12.8px"><br></span></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><span style="font-size:12.8px;font-family:arial,sans-serif">Animate.fadeIn(view, withSpeed: transitionSpeed.fast)</span></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><br></div><div class="gmail_extra"><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif">even if you had only one possible enum value over all declared enums because that would still force the compiler to search for each value over all known enums to define where the value you're using comes from and make sure there are no two enums with the same value.</div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif"><br></div><div class="gmail_default" style="font-family:'trebuchet ms',sans-serif">Aside from that, I good with the idea.</div><br clear="all"><div><div dir="ltr"><br></div></div>
<br><div class="gmail_quote">On 13 May 2016 at 15:09, Eric Miller via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>This might open a larger can of worms than I imagine, but what do you folks think about using the `rawValue` of an enum when that rawValue is a fit for the expected type?</div><div><br></div><div>Use case.</div><div><br></div><div>I'm making an animation facade, and it has some speed presets:</div><div><br></div><div>class Animate { </div><div> enum transitionSpeed: NSTimeInterval {</div><div> case fast = 0.15</div><div> case slow = 0.5</div><div> }</div><div> enum ambientAnimationSpeed: NSTimeInterval {</div><div> case fast = 1.0</div><div> case slow = 5.0</div><div> }</div><div> ...</div><div>}</div><div><br></div><div>I did them with static variables at first but that made the call site verbose. Compare:</div><div><br></div><div>Animate.fadeIn(view, withSpeed: Animate.cfg.transitionFast)</div><div>Animate.fadeIn(view, withSpeed: .fast)</div><div><br></div><div>So, I like the enum approach better, but my library code has to use `rawValue` to do anything with the actual value, of course:</div><div><br></div><div>static func fadeIn(view: UIView?, withSpeed duration:transitionSpeed = .fast) {</div><div> ...</div><div> UIView.animateWithDuration(duration.rawValue, animations: { })</div><div>}</div><div><br></div><div>It's not a serious issue, but the code is more clear and beautiful if it has just myIntent, rather than myIntent.rawValue.</div><div><br></div><div>I've hit this issue when modeling other things, such as:</div><div><br></div><div>* server fault codes</div><div>* HTTP status codes</div><div>* Currency codes</div><div>* Days of the week</div><div><br></div><div>Would it be appropriate to "autocast" to the rawValue of the enum when the rawValue's Type matches the type expectation of the API? Or would this introduce a bunch of type system uncertainty?</div><div><br></div><div>Maybe this could be implemented as a protocol? It feels almost like the convenience of `CustomStringConvertible`'s `description` property.</div></div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>