Re: [swift-users] Bool to Int

2016-11-22 Thread Kenny Leung via swift-users
Hi Marc. My old mechanical engineering prof used to say, “C is easy if you know assembler”. The fact that such a simple construct does not work and requires such a long explanation - which may still not be understood by a newbie - is a problem that should be addressed. Even this explanation

Re: [swift-users] Bool to Int

2016-11-21 Thread Adrian Zubarev via swift-users
Where is your problem here? It’s simple and easy ;) extension Integer { init(_ boolean: Bool) { self = boolean ? 1 : 0 } } Int(10 > 4) UInt32(1 <= 2) --  Adrian Zubarev Sent with Airmail Am 22. November 2016 um 00:54:47, Rick Mann via swift-users

Re: [swift-users] Bool to Int

2016-11-21 Thread Rick Mann via swift-users
> On Nov 21, 2016, at 15:09 , Marco S Hyman wrote: > >> Except it does, because if I write >> >> let a = 2 > >> a is of type Int (at least, according to Xcode's code completion). > > and if you write > > let b = 2 + 0.5 > > 2 is treated as a double. The type of

Re: [swift-users] Bool to Int

2016-11-21 Thread Marco S Hyman via swift-users
> Except it does, because if I write > > let a = 2 > a is of type Int (at least, according to Xcode's code completion). and if you write let b = 2 + 0.5 2 is treated as a double. The type of the literal “2” varies with context. Do you also find that inconsistent and confusing?

Re: [swift-users] Bool to Int

2016-11-21 Thread Roderick Mann via swift-users
> On Nov 21, 2016, at 13:14 , Nevin Brackett-Rozinsky > wrote: > > I don’t see what there is to be confused about. > > A “literal” is literally a bunch of characters in source code. The compiler > interprets those characters as representing whatever type is

Re: [swift-users] Bool to Int

2016-11-21 Thread Nevin Brackett-Rozinsky via swift-users
I don’t see what there is to be confused about. A “literal” is literally a bunch of characters in source code. The compiler interprets those characters as representing whatever type is appropriate to the context. For the case at hand, a boolean literal can be interpreted as any type which

Re: [swift-users] Bool to Int

2016-11-21 Thread Rick Mann via swift-users
> On Nov 21, 2016, at 09:46 , Kenny Leung via swift-users > wrote: > > This is so confusing. "Literals are untyped", but there’s a “BooleanLiteral”, > which is obviously of type Boolean. Agreed. -- Rick Mann rm...@latencyzero.com

Re: [swift-users] Bool to Int

2016-11-21 Thread Adrian Zubarev via swift-users
A literal doesn’t have a type on its own. Instead, a literal is parsed as having infinite precision and Swift’s type inference attempts to infer a type for the literal. Source --  Adrian Zubarev Sent with Airmail Am 21. November 2016 um 18:46:32, Kenny Leung via swift-users

Re: [swift-users] Bool to Int

2016-11-21 Thread Kenny Leung via swift-users
This is so confusing. "Literals are untyped", but there’s a “BooleanLiteral”, which is obviously of type Boolean. -Kenny > On Nov 21, 2016, at 2:49 AM, Adrian Zubarev via swift-users > wrote: > > In general this is a correct behaviour, because literals in Swift are >

Re: [swift-users] Bool to Int

2016-11-20 Thread Marco S Hyman via swift-users
> Is there a way that avoids branching? Don’t know. Have you profiled let r = a > b ? 1 : 0 to know it is an issue? > > 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

Re: [swift-users] Bool to Int

2016-11-20 Thread Rick Mann via swift-users
> On Nov 20, 2016, at 19:52 , Jon Shier 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?

Re: [swift-users] Bool to Int

2016-11-20 Thread Jon Shier via swift-users
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. Jon > On Nov 20, 2016, at 10:48 PM, Rick Mann via swift-users > wrote: > > It seems I

[swift-users] Bool to Int

2016-11-20 Thread Rick Mann via swift-users
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 rm...@latencyzero.com ___ swift-users mailing