<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>The throwing ones might use "strict", or something similar, i.e.</div><div><br></div><div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">init(strict value: Int64) throws</span></font></div></div><div><br></div><div>-Thorsten</div><div><br>Am 07.12.2015 um 00:55 schrieb Dmitri Gribenko via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><span>On Sun, Dec 6, 2015 at 11:28 AM, Matthew Johnson via swift-evolution</span><br><span><<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:</span><br><blockquote type="cite"><span>Problem:</span><br></blockquote><blockquote type="cite"><span>Swift numeric types all currently have a family of conversion initializers. In many use cases they leave a lot to be desired. Initializing an integer type with a floating point value will truncate any fractional portion of the number. Initializing with an out-of-range value traps.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Solution:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Sometimes it would be more desirable to convert the runtime value if it can be done without losing information (or possibly with only minimal loss of precision when initializing a floating point type). This could be easily accomplished if the standard library had a family of failable initializers for all numeric types, either returning an Optional or throwing when the initialization was not successful.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I prefer the throwing version because failure can be automatically propagated up the call stack and the error could capture value that was provided and the type that failed to initialize which may be useful when debugging. Also, `try?` allows callers to throw away the error if the detail isn’t necessary. However, the Optional version would provide the basic functionality that is desired and would be sufficient if the community likes it better.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>// Conversions from all integer types.</span><br></blockquote><blockquote type="cite"><span>init?(_ value: Int8)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: Int16)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: Int32)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: Int64)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: Int)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: UInt8)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: UInt16)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: UInt32)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: UInt64)</span><br></blockquote><blockquote type="cite"><span>init?(_ value: UInt)</span><br></blockquote><span></span><br><span>One issue is that these initializers already exist with the signature</span><br><span>'init(_ value: IntXYZ)'. Adding these failable initializers would</span><br><span>make code like the following ambiguous:</span><br><span></span><br><span>var u8 = getUInt8()</span><br><span>var myInt = Int(u8)</span><br><span></span><br><span>We need a label to distinguish these.</span><br><span></span><br><span>Dmitri</span><br><span></span><br><span>-- </span><br><span>main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if</span><br><span>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>>*/</span><br><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>