[swift-users] Quick Question about ExpressibleByStringLiteral

David Sweeris davesweeris at mac.com
Thu Mar 9 19:59:13 CST 2017


If my type doesn’t know/care about the difference between a normal “String" and an “ExtendedClusterScalarGraphemeLiteralUnicodeTypeCluster” (or whatever those other literal types are called), is there anything wrong with doing this?
public protocol EasilyExpressibleByStringLiteral : ExpressibleByStringLiteral {
    typealias StringLiteralType = String
}
extension EasilyExpressibleByStringLiteral where StringLiteralType == String {
    public init(unicodeScalarLiteral value: String.UnicodeScalarLiteralType) {
        self.init(stringLiteral: String(describing: value))
    }
    public init(extendedGraphemeClusterLiteral value: String.ExtendedGraphemeClusterLiteralType) {
        self.init(stringLiteral: String(describing: value))
    }
}
because then I only have to write the one init function:
public struct MyType : EasilyExpressibleByStringLiteral {
    public init(stringLiteral value: StringLiteralType) {...}
}
and the compiler will stop complaining about my type not conforming to the other two protocols. Because I’ve scanned the docs, and I can’t even figure out how to create an ExtendedGraphemeClusterLiteral, let alone come up with a reason why I’d want to treat it differently than a regular String when using it to initialize an instance of MyType.

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170309/acb11b1c/attachment.html>


More information about the swift-users mailing list