<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> foo_impl(value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int32</span><span style="font-variant-ligatures: no-common-ligatures" class="">) { </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">/* ... */</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> foo_impl(value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int64</span><span style="font-variant-ligatures: no-common-ligatures" class="">) { </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">/* ... */</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> foo(value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">#if</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">arch</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">i386</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) || </span><span style="font-variant-ligatures: no-common-ligatures" class="">arch</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">arm</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; foo_impl(value: Int32(value))</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">#else</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">foo_impl</span><span style="font-variant-ligatures: no-common-ligatures" class="">(value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int64</span><span style="font-variant-ligatures: no-common-ligatures" class="">(value))</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">#endif</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><br class=""><div style=""><blockquote type="cite" class=""><div class="">On Nov 23, 2016, at 1:31 PM, Martin R via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I wonder what the best way would be to call a specialized function dependent on the size of `Int`. Let's say that I have two implementations<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo_impl(value: Int32) { /* ... */ }<br class=""> &nbsp;&nbsp;&nbsp;func foo_impl(value: Int64) { /* ... */ }<br class=""><br class="">and I want <br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo(value: Int)<br class=""><br class="">to call the "right one" of them, according to the architecture (32-bit or 64-bit).<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo(value: Int) { foo_impl(value: value) }<br class=""><br class="">does not compile. (I assume that is because `Int` is not a type alias to `Int32` or `Int64` but an independent type.)<br class=""><br class="">This works: <br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo1(value: Int) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if MemoryLayout&lt;Int&gt;.size == 4 {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo_impl(value: Int32(value))<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo_impl(value: Int64(value))<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class="">or<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo2(value: Int) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch MemoryLayout&lt;Int&gt;.size {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 4: foo_impl(value: Int32(value))<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 8: foo_impl(value: Int64(value))<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;abort()<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class="">But a typo in the constants would go unnoticed and just call the wrong function, or cause a runtime error instead of a compile-time error. And perhaps `Int` can be an 128-bit integer in the future? The compiler would not warn that the code needs to be updated.<br class=""><br class="">This seems to be more promising:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo3(value: Int) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (__WORDSIZE) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 32: foo_impl(value: Int32(value)) // Warning: Will never be executed<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 64: foo_impl(value: Int64(value))<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class="">Apparently the compiler "knows" which case will be executed, `foo3` does not compile if there is no case matching actual integer size. But there is always an annoying warning for the unused case. And I am not sure if it is guaranteed that __WORDSIZE is the number of bits in an `Int`.<br class=""><br class="">So my question is: What would be the best way to dispatch dependent on the size of `Int`, such that<br class=""><br class="">- The compiler checks the correctness.<br class="">- The compiler optimizes the code so that no runtime check is done.<br class="">- No warnings are produced.<br class=""><br class="">If `Int` had a `native` property like `CGFloat` then I could simply call<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func foo(value: Int) { foo_impl(value: value.native) }<br class=""><br class="">but I could not find such a property. (Would that be a reasonable thing to ask on swift-evolution?)<br class=""><br class="">Background: I am asking this just out of curiosity, but the question came up when looking at the `hash_combine` function in the Boost library:<br class=""><br class=""> &nbsp;<a href="http://www.boost.org/doc/libs/1_62_0/boost/functional/hash/hash.hpp" class="">http://www.boost.org/doc/libs/1_62_0/boost/functional/hash/hash.hpp</a> <br class=""><br class="">with quite different implementations <br class=""><br class=""> &nbsp;&nbsp;&nbsp;inline void hash_combine_impl(boost::uint32_t&amp; h1, boost::uint32_t k1)<br class=""> &nbsp;&nbsp;&nbsp;inline void hash_combine_impl(boost::uint64_t&amp; h, boost::uint64_t k)<br class=""><br class="">and I wondered how this would be done in Swift.<br class=""><br class="">Regards, <br class="">Martin<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></div></blockquote></div><br class=""></body></html>