Skip to content

Instantly share code, notes, and snippets.



Last active Jul 9, 2017
What would you like to do?

Extending Swift Literals

  • Proposal: TBD
  • Author: Erica Sadun
  • Status: TBD
  • Review manager: TBD


This proposal expands Swift's language literals to include common cross-platform concepts.


A Swift literal represents a fixed value in source code. A literal can be a string, a number (for example an integer), a compound value (such as an array), or one of several predefined "playground" literals including colors, resource file paths, and resource images.

Swift literals do not have types. They are universal representations that are evaluated and their types inferred from the context in which they are used. Because their nature is typeless, the same color literal can initialize UIColor, NSColor, and SKColor instances. The type cannot be inferred from the source without the context of its destination.

let color = #colorLiteral(red: 0.8100712299, green: 0.1511939615, blue: 0.4035313427, alpha: 1)

Detailed Design

Namespace redesign
Kind Literal Parameters
Color `#literal.color(red:, green:, blue:, alpha:)` floating point values
Image `#literal.image(resourceName:)` String with resource name
File `#literal.file(resourceName:)` String with resource name
Kind Literal Parameters
Sound `` String with resource name
URL `#literal.url(string:)`, `#literal.url(filePath:)` String with resource location
View `#literal.view(frame:)` floating point values for x, y, width, height
Font `#literal.font(face:, size:)` string, floating point
Date `` floating point offset from Unix epoch
Unicode `#literal.unicode(name:)` Official unicode name, e.g. `#literal.unicode(name:"DOG FACE")`
Regex `#literal.regex(pattern:)` Regular expression string
Kind Literal Parameters
Point `#literal.point(x:, y:)`, `#literal.point(x:, y:, z:)`, `#literal.point(x:, y:, z:, w:)` floating point values
Vector `#literal.vector(dx:, dy:)`, `#literal.vector(dx:, dy:, dz:)`, `#literal.vector(dx:, dy:, dz:, dw:)` floating point
Size `#literal.size(width:, height:)`, `#literal.size(width:, height:, depth:)` floating point
Rect `#literal.rect(x:, y:, width:, height:)` floating point
Affine Transform `#literal.affineTransform(a:,b:,c:,d:,tx:,ty:)`, `#literal.affineTransform(translateX:, translateY:)`, `#literal.affineTransform(scaleY:, scaleY:)`, `#literal.affineTransform(rotation:)`, floating point
Bezier Path `#literal.bezier("M56 75Q67 53 91 45A18 18 0 1 1 127 40C170 29 193 62 212 173L225 110Q241 70 252 120L253 156C253 180 265 223 235 214Q210 210 215 242C222 265 161 249 125 248L102 245Q78 229 110 216L170 207C139 183 40 199 41 109A18 18 0 1 1 56 75Z")` String with SVG path notation

Not included:

Attributed Strings: I would like to see a way to define attributed strings (using some system like CSS/HTML) but could not think up a simple representation similar to the others mentioned in the preceding table.

JSON Literals: Again, probably too complex and possibly not worth their weight. If they could exist, they'd have to be imported via a resource or URL and transformed to a local type.


Acting as points of inspiration, here is the current declaration for valid quick look types.

public enum PlaygroundQuickLook {
    case text(String)
    case int(Int64)
    case uInt(UInt64)
    case float(Float32)
    case double(Float64)
    case image(Swift.Any)
    case sound(Swift.Any)
    case color(Swift.Any)
    case bezierPath(Swift.Any)
    case attributedString(Swift.Any)
    case rectangle(Float64, Float64, Float64, Float64)
    case point(Float64, Float64)
    case size(Float64, Float64)
    case bool(Bool)
    case range(Int64, Int64)
    case view(Swift.Any)
    case sprite(Swift.Any)
    case url(String)
    case _raw([UInt8], String)

Impact on Existing Code

This proposal is purely additive.

Alternatives Considered

Using distinct literal names without subsuming them into a namespaced umbrella.


This comment has been minimized.

Copy link

@GlennHowes GlennHowes commented Jul 12, 2016

The problem with fonts is that you rarely want an explicit face, you want a system font, a preferred font or one that corresponds to a descriptor.


This comment has been minimized.

Copy link

@grhowes grhowes commented Oct 5, 2016

If it's at all helpful, I have written an license compatible pure Swift implementation of an SVG Path parser:


This comment has been minimized.

Copy link

@JetForMe JetForMe commented Dec 17, 2016

Color literals don't specify anything about the color space or gamut. I know CGColor and friends have various color space complexities. I'd be okay with "device" gamut, but it should be clear what's happening when they're used.


This comment has been minimized.

Copy link
Owner Author

@erica erica commented Dec 18, 2016

I like the idea of "device" gamut being understood. (By the way, color literals already exist in Swift)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment