[swift-users] Quick Question about ExpressibleByStringLiteral

David Sweeris davesweeris at mac.com
Thu Mar 9 20:07:04 CST 2017


Great! Thanks, Andrew :-)

- Dave Sweeris

> On Mar 9, 2017, at 6:00 PM, Jordan Rose <jordan_rose at apple.com> wrote:
> 
> Andrew Bennett is two steps ahead of you. :-) https://github.com/apple/swift/pull/7125 <https://github.com/apple/swift/pull/7125>
> 
> Jordan
> 
>> On Mar 9, 2017, at 17:59, David Sweeris via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> 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
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

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


More information about the swift-users mailing list