Mandatory Disclaimer: This does not fall under the scope of Swift 3 timeframe as it is entirely additive
I love literals. Right now, in addition to the protocols that allow types to adopt literal expression, Swift offers three special literals: #colorLiteral(red:,green:,blue:alpha:)
, #fileLiteral(#imageLiteral(resourceName:)
, and #imageLiteral(resourceName:)
.
Literals do not have types. They are evaluated and their types inferred from the context in which they are used. For example:


I literally (sorry, pun intended) cut and paste the code from the iOS playground to the OS X/macOS playground.
The same source line (let color = #colorLiteral(red: 0.8100712299, green: 0.1511939615, blue: 0.4035313427, alpha: 1)
) established a UIColor
in one case and an NSColor
in the other. In both examples, the integer literal “1” resolved to Double
type inference.
Introducing language literals creates typeless universal representations that cross operating systems and exist independent of implementation details. Examples of these typeless concepts include numbers, file paths, images, and colors (not to mention arrays, truth values, etc).
So what can Swift introduce next?
While I’d love to see Bezier paths, I’m not sure there’s a way to do this cleanly. If you notice, in the existing literals, all fields are declared in the initializer. Although Bezier paths use essentially universal components, they require multistage creation.
A Bezier path consists of a series of move to, line to, quad curve to, cubic curve to, and close. (Other features like “remove all points” are simply gravy.) If Swift could offer a literal Bezier, at best it could construct a single empty instance, or a fixed set of predefined instances like an oval, square, line. Neither is particularly satisfying:
let myBezier = #bezierLiteral()
let myBezier = #bezierLiteral(.oval, rect:)
There is, as far as I know, no such thing as a typeless method that would allow universal construction:
let myBezier = #bezierLiteral()
myBezier.#bezierMoveTo(point:)
myBezier.#bezierLineTo(point:)
You could work around this by limiting Bezier literals to load from common vector graphic formats stored in a file, just as #imageLiteral
now allows you to load graphics. If you really want to stretch, you could take that same approach to load style sheets, enabling you to construct attributed strings. (Or introduce #jsonLiteral
)
Moving to more reasonable and achievable goals, I think Swift could easily allow literals for core geometry (rects, points, sizes, vectors, transforms), that would apply across 2D and 3D applications, allowing you to use the same literals regardless of whether you’re using Quartz, Accelerate, GameplayKit, simd, and so forth.
I think Swift would also allow simple adoption of literals for sounds, URLs, and dates (at least those using the Unix epoch, rather than Apple’s epoch).
What other targets of opportunity can you think of that would simplify cross-platform development through Swift literals? Let me know.