<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="">Hi guys, <div class=""><br class=""></div><div class="">I’m either missing a subtlety in regards to closures or I found a bug with the type inference system in Xcode 8 Beta 6. Thought I’d run it by you.</div><div class=""><br class=""></div><div class="">I’m trying to use Data’s method withUnsafeBytes(_:). It’s declared as:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> withUnsafeBytes<ResultType, ContentType>(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> body: (</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">UnsafePointer</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">ContentType</span><span style="font-variant-ligatures: no-common-ligatures;" class="">>) </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">ResultType</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">rethrows</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">ResultType</span></p><div class=""><br class=""></div></div><div class="">The Swift Language book states that closure syntax uses the following form:</div><div class=""><br class=""></div><div class=""><p 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=""></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">{ (parameters) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">returntype</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">in</span></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> statements</span></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></p></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">So, at the most basic implementation, I tried the following:</span></div><div class=""><br class=""></div><div class=""><p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> tryThis = items.withUnsafeBytes {</span></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> (bytes) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</span></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> 1</span></p>
<p style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></p><div class=""><br class=""></div></div><div class="">And I get the error <font face="Menlo" class="">Declared closure result 'Int' is incompatible with contextual type ‘ResultType’.</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">Xcode suggests I change <font face="Menlo" class="">Int</font> to <font face="Menlo" class="">ResultType</font>, but that just gives me the error that I’m using an undeclared type. </div><div class=""><br class=""></div><div class="">Am I getting the syntax wrong or Xcode getting confused?</div><div class=""><br class=""></div><div class="">Thanks a lot. </div><div class=""><br class=""></div><div class="">—</div><div class="">Chris McIntyre</div></body></html>