Quick Question about ExpressibleByStringLiteral

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

Andrew Bennett is two steps ahead of you. :-) Simplifying implementation of `ExpressibleByStringLiteral` by therealbnut · Pull Request #7125 · apple/swift · GitHub

Jordan

···

On Mar 9, 2017, at 17:59, David Sweeris via swift-users <swift-users@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Great! Thanks, Andrew :-)

- Dave Sweeris

···

On Mar 9, 2017, at 6:00 PM, Jordan Rose <jordan_rose@apple.com> wrote:

Andrew Bennett is two steps ahead of you. :-) https://github.com/apple/swift/pull/7125

Jordan

On Mar 9, 2017, at 17:59, David Sweeris via swift-users <swift-users@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users