<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="">That depends... Does “x = a ? b : c” and "if a {x = b} else {x = c}” compile down to the same machine code? I know of one arch where “?:” takes 0 cycles, but I don’t know if there’s a difference on x86 or ARM.<div class=""><br class=""></div><div class="">Either way, I’d think that just doing the comparison within `sign` would be faster, since it doesn’t create two extra Bools and SignedNumberTypes.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 24, 2016, at 6:33 AM, Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 23 May 2016, at 20:19, Dany St-Amant via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Challenge accepted… Removed the if/else if, at the cost of double function call to a tri-op:</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">extension<span class="" style=""> </span><span class="" style="color: rgb(112, 61, 170);">Bool</span><span class="" style=""><span class="Apple-converted-space"> </span>{</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>as01<T:<span class="" style="color: rgb(112, 61, 170);">SignedNumberType</span>>() -><span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">T</span><span class="Apple-converted-space"> </span>{<span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">self</span><span class="Apple-converted-space"> </span>?<span class="Apple-converted-space"> </span><span class="" style="color: rgb(39, 42, 216);">1</span><span class="Apple-converted-space"> </span>:<span class="Apple-converted-space"> </span><span class="" style="color: rgb(39, 42, 216);">0</span><span class="Apple-converted-space"> </span>}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(187, 44, 162);">extension</span><span class="" style=""> </span>SignedNumberType<span class="" style=""><span class="Apple-converted-space"> </span>{</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>sign:<span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Self</span><span class="Apple-converted-space"> </span>{<span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>(<span class="" style="color: rgb(187, 44, 162);">self</span><span class="Apple-converted-space"> </span><span class="" style="color: rgb(61, 29, 129);">></span><span class="Apple-converted-space"> </span><span class="" style="color: rgb(39, 42, 216);">0</span>).<span class="" style="color: rgb(49, 89, 93);">as01</span>()<span class="Apple-converted-space"> </span><span class="" style="color: rgb(61, 29, 129);">-</span><span class="Apple-converted-space"> </span>(<span class="" style="color: rgb(187, 44, 162);">self</span><span class="Apple-converted-space"> </span><span class="" style="color: rgb(61, 29, 129);"><</span><span class="Apple-converted-space"> </span><span class="" style="color: rgb(39, 42, 216);">0</span>).<span class="" style="color: rgb(49, 89, 93);">as01</span>() }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div></div></blockquote></div><br class=""><div class="">I don’t believe this solves the problem; I think you’ve really just moved the branching into the as01() method.</div><div class=""><br class=""></div><div class="">I think the more correct solution would be for all integer types to have a required Bool initialiser in a protocol somewhere, this way you could just do:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var sign:Self { return Self(boolValue: self > 0) - Self(boolValue: self < 0) }</font></div><div class=""><br class=""></div><div class="">Since all integer types should be able to do this by just extending the value to fit, rather than testing it. I’m not completely up-to-date on the integer changes that will be in Swift 3, but perhaps conversion from bool will be easier in future, in Swift 2.2 it’s not really treated as a number type for conversion purposes, which is disappointing.</div><div class=""><br class=""></div><div class="">Also apologies for my initial misunderstanding of the need for this sign property, no idea how I managed to get such a mental block as it’s obviously very useful for multiplication when account for sign and a few other cases. Anyway, I’m a +1, though implementation is definitely trickier than it seems it should be, thanks to some of Swift’s number-related quirks!</div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>