ExpressibleByLiteralSyntax
A Swift type whose value can be represented directly in source code by a Swift literal.
protocol ExpressibleByLiteralSyntax
Browse conforming typesConforming types do not contain Swift source code; rather, they can be expressed in Swift source code, and this protocol can be used to get whatever source code would do that. For example, String
is ExpressibleByLiteralSyntax
but StringLiteralExprSyntax
is not.
This protocol is usually not used directly. Instead, conforming types can be turned into syntax trees using init(literal:)
:
let expr2 = Expr(literal: [0+1, 1+1, 2+1])
// `expr2` is a syntax tree for `[1, 2, 3]`.
Or interpolated into a Swift source code literal with the syntax \(literal: <value>)
:
let greeting = "Hello, world!"
let expr1 = ExprSyntax("print(\(literal: greeting))")
// `expr1` is a syntax tree for `print("Hello, world!")`
Note that quote marks are automatically added around the contents of string literals; you don’t have to write them yourself. The conformance for String
will automatically ensure the contents are correctly escaped, possibly by using raw literals or other language features:
let msPath = "c:\\windows\\system32"
let expr3 = ExprSyntax("open(\(literal: msPath))")
// `expr3` might be a syntax tree for `open(#"c:\windows\system32"#)`
// or for `open("c:\\windows\\system32")`.
Other conformances have similar intelligent behaviors: floating-point types produce correct syntax trees for infinities and NaNs, nested optionals produce .some(nil)
where appropriate, etc.