<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">My use case is a small chess game where my Board class has a Location indexer, and any Location object has a guaranteed value on the Board. The Location struct handles adding signed deltas to coordinates and returns nil when that location wouldn't exist. My coordinates are UInt8 values and the "casting" code looks like:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(101, 123, 131);" class=""><span style="color: rgb(133, 153, 1);" class="">private</span> <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">func</span> intToU8(int: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Int</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">UInt8</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(101, 123, 131);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">if</span> int >= <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">0</span> && int <= <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Int</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">UInt8</span>.max) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(101, 123, 131);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">UInt8</span>(int)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(101, 123, 131);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(133, 153, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #657b83" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span>return<span style="font-variant-ligatures: no-common-ligatures; color: #657b83" class=""> </span>nil</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(101, 123, 131);" class="">}</div></div><div class=""><br class=""></div><div class="">Of course, if UInt8's initializer returned an Optional instead of trapping, I wouldn't need that function at all.</div><div class=""><br class=""></div><div class="">In general, I would also say that it's a good idea to make dangerous behavior explicit. I think that Swift's model for converting integers is already punishing enough that we might as well add the exclamation mark.<br class=""><div class="">
<br class="Apple-interchange-newline"><span style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;" class="">Félix</span>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Le 12 févr. 2016 à 12:27:55, Javier Soto <<a href="mailto:javier.api@gmail.com" class="">javier.api@gmail.com</a>> a écrit :</div><br class="Apple-interchange-newline"><div class="">I'm curious: how would you use the failable initializers? I feel like, more often than not, you would end up force-unwrapping the return value, which would produce the same result as trapping inside. But I guess in that case you would make it clearer on the caller site that that can fail?<br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Feb 12, 2016 at 9:23 AM Félix Cloutier <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">It appears that the current integer conversion initializers will either always work, possibly trap, or truncate:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">init(_: Int)<br class="">init(_: Int8)<br class="">init(_: Int16)<br class="">init(_: Int32)<br class="">init(_: Int64)<br class="">init(_: UInt)<br class="">init(_: UInt8)<br class="">init(_: UInt16)<br class="">init(_: UInt32)<br class="">init(_: UInt64)<br class="">init(truncatingBitPattern: Int)<br class="">init(truncatingBitPattern: Int16)<br class="">init(truncatingBitPattern: Int32)<br class="">init(truncatingBitPattern: Int64)<br class="">init(truncatingBitPattern: UInt)<br class="">init(truncatingBitPattern: UInt16)<br class="">init(truncatingBitPattern: UInt32)<br class="">init(truncatingBitPattern: UInt64)<br class=""></blockquote><div class=""><br class=""></div><div class="">I suggest that we change trapping initializers to failable initializers. Initializers that can't fail (identity, unsigned -> bigger signed/unsigned, signed -> bigger signed) should keep a non-Optional type.</div><div class=""><br class=""></div><div class="">This could extend to changing SignedIntegerType's and UnsignedIntegerType's (U)IntMax to failable initializers as well.</div><div class=""><br class=""></div><div class="">I think I remember someone on the core team saying that someone's working on integer types at the moment. Is that right?</div><div class="">
<br class=""><span style="font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; float: none; display: inline !important;" class="">Félix</span>
</div>
<br class=""></div></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div><div dir="ltr" class="">-- <br class=""></div><div dir="ltr" class="">Javier Soto</div>
</div></blockquote></div><br class=""></div></body></html>