One of the best things of Swift is the Eat your own dog food philosophy. There is no black magic behind the curtains. A special case that I really love, is Literals.

Int is nothing more than a struct, and yet, instead of

let i = Int(42)

We can say

let i = 42

There is no special glue, but a simple protocol. Int adopts SignedNumber, which, in turns adopts ExpressibleByIntegerLiteral. A really simple protocol that defines an associatedtype and a a special init(integerLiteral value: Self.IntegerLiteralType)

The expression let i = 42 is funneled trough init(integerLiteral:), that’s all.

And, Int is not special, not in this regard. Any type can adopt ExpressibleByIntegerLiteral.

struct Seats {
    let seats:Int

extension Seats: ExpressibleByIntegerLiteral {
    init(integerLiteral value: Int) {
        self.seats = value

extension Seats: CustomStringConvertible {
    var description:String {
        get {
            return "number of seats: \(self.seats)"

let s:Seats = 42

print(s) // number of seats: 42

There are a number of Literals protocols


struct SoldOut {
    let soldOut:Bool

extension SoldOut:ExpressibleByBooleanLiteral {
    init(booleanLiteral value: Bool) {
        self.soldOut = value

let so:SoldOut = false
print(so) // SoldOut(soldOut: false)

The case for strings is particularly interesting because it clearly illustrates the power of protocols. We are working with structs. In principle, there is no concept of heritance in value types. But protocols can give us a simple way to make sure different types express themselves the same way. No, is not polymorphism, but is still handy to know that regardless of the actual type, a bunch of objects can respond to a message with the same signature. StringLiteralConvertible adopts ExtendedGraphemeClusterLiteralConvertible which in turns adopts UnicodeScalarLiteralConvertible. If a struct S adopts StringLiteralConvertible, is also adopting UnicodeScalarLiteralConvertible. And because methods defined in protocols are, by default, required, you have to define all three inits. A wonderful, as usual, demonstration of StringLiteralConvertible can be found in NSHipster.

A special case is ExpressibleByNilLiteral. This is adopted by Optional which will assign .none when value is nil, or .some otherwise. But since nil has a specific meaning in Swift, Apple discourage the adoption of the protocol.