Swift Expressible Literal
This is the English version of my post at cnblogs.com.
A Question
Given the code bellow:
|
|
Is there a way to make it work?
So, is there a way to implicitly create a struct from a string literal?
Expressible Literal Protocol
The answer is yes, with the the magic of expressible literal protocol
. It’s a suite of protocols which includes:
protocol | literal |
---|---|
ExpressibleByNilLiteral | nil |
ExpressibleByBooleanLiteral | boolean literal |
ExpressibleByIntegerLiteral | interger literal |
ExpressibleByFloatLiteral | float literal |
ExpressibleByUnicodeScalarLiteral | unicode scalar literal |
ExpressibleByExtendedGraphemeClusterLiteral | extended grapheme cluster literal |
ExpressibleByStringLiteral | string literal |
ExpressibleByStringInterpolation | interpolated string literal |
ExpressibleByArrayLiteral | array literal |
ExpressibleByDictionaryLiteral | dictionary literal |
In this case, adding ExpressibleByStringLiteral
conformance can make the implicit instantiation possible.
|
|