[swift-users] Bool to Int

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Mon Nov 21 15:14:52 CST 2016


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.

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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161121/0fbdd463/attachment.html>


More information about the swift-users mailing list