[swift-users] Bool to Int

Rick Mann rmann at latencyzero.com
Sun Nov 20 22:37:00 CST 2016


> On Nov 20, 2016, at 19:52 , Jon Shier <jon at jonshier.com> wrote:
> 
> Except in that case true isn’t a Bool but an NSNumber, which is why you can initialize an Int from it. It seems trivially easy to add an Int extension to do what you want though.

Is there a way that avoids branching?

So, according to Xcode, "true" and "a > b" both have type "Bool". I don't know why the compiler allows one and not the other, except that it's literal, and I guess there's a "BoolLiteralConvertible" (or equivalent) for the types.

For now I'm doing what I need with branching, but it would be nice to find a more efficient way.

> 
> 
> Jon
> 
>> On Nov 20, 2016, at 10:48 PM, Rick Mann via swift-users <swift-users at swift.org> wrote:
>> 
>> It seems I can't do this:
>> 
>> let r = Int(a > b)
>> 
>> but I can do it with a literal:
>> 
>> let r = Int(true)
>> 
>> I'd like to do this to implement signum without branching, but perhaps that's not possible.
>> 
>> -- 
>> Rick Mann
>> rmann at latencyzero.com
>> 
>> 
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 


-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list