[swift-users] Bool to Int

Roderick Mann rmann at latencyzero.com
Mon Nov 21 15:20:42 CST 2016


> On Nov 21, 2016, at 13:14 , Nevin Brackett-Rozinsky <nevin.brackettrozinsky at gmail.com> 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 appropriate to the context.
> 
> For the case at hand, a boolean literal can be interpreted as any type which conforms to the ExpressibleByBooleanLiteral protocol. If the context provides no information, the compiler defaults to interpreting a boolean literal as representing a Bool.
> 
> The situation is similar for every other kind of literal. For example, “2” defaults to being interpreted as an Int, but if the context requires a Double then it will be interpreted as a Double. The text “2” does not have a type of its own.

Except it does, because if I write

	let a = 2

a is of type Int (at least, according to Xcode's code completion). But this gives inconsistent results:

	let t = true

	let a = Int(true)
	let b = Int(t)		//  Error

I find this to be very inconsistent and confusing.

> 
> Nevin
> 
> 
> On Mon, Nov 21, 2016 at 3:55 PM, Rick Mann via swift-users <swift-users at swift.org> wrote:
> 
> > On Nov 21, 2016, at 09:46 , Kenny Leung via swift-users <swift-users at swift.org> wrote:
> >
> > This is so confusing. "Literals are untyped", but there’s a “BooleanLiteral”, which is obviously of type Boolean.
> 
> Agreed.
> 
> --
> 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