Apply implicit ExpressibleByStringLiteral initializer to returned value


struct Categories: ExpressibleByStringLiteral {
    var category: String = ""

    init(category: String) {
        self.category = category
    init(stringLiteral value: String) {
        self = .init(category: value)
    static func abc(_ value: String) -> Categories {
        // Error: Cannot convert value String to Categories.

I got error in abc function.
But, hey, It is not true, I could convert value String to Categories.

Is there any pitch/proposal for this functionality?

My misunderstanding of this protocol purpose.

Desired usage would be:

extension Categories {
  static let myCategory: Self = "MyCategory"

Notice that the protocol is named ExpressibleByStringLiteral, not ExpressibleByString

Literals are different things than the specific types, check for example this code that uses dictionary literal, but doesn't use a Dictionary

let pairs: KeyValuePairs<Int, String> = [
    0: "zero",
    0: "nothing",
    1: "one",
print(pairs) // prints [0: "zero", 0: "nothing", 1: "one"]

It's not a Dictionary that is converted into KeyValuePairs because Dictionary cannot contain repeated keys (0 in this example)

Even though the literal is called "dictionary literal" or "string literal" they aren't Dictionaries or Strings

Swift tries to be a strongly typed language, and a proposal like that would go against that. I don't think it would get much support


ExpressibkeByStringLiteral is meant to do exactly what the name suggests, that is, allow the type to be initialized from a string literal. It’s not supposed to be an implicit String conversion protocol, and it’s an explicit design decision of Swift to not allow implicit type conversions. The idiomatic way to express a type conversion is with a single-argument, no-label initializer:

extension Categories {
    init(_ string: String) {
        self.category = string

Indeed: not supporting implicit type conversions is explicitly an overarching design principle in Swift.


Ah, I see.

Common use case of this protocol would be ( in my example )

extension Categories {
  static let someCategory: Self = "myCategory"