String literal is somehow converted into a different type

Sorry for the poorly worded title, I am not sure how to explain this well without an example.


The question is pertaining to Swift and not to Apple framework.


In the below code I am able to initialise the variable phrase1 with a string literal and it somehow converts into AppShortcutPhrase<DummyIntent>


  1. Is this language feature? If so what is it called and is there any documentation about it? Also is this new feature after Swift 5?
  2. Is this possible only with string literals or any type could be converted to another type implicitly if there was a suitable initialiser or implementation?
  3. How do I do this for my own struct?
import AppIntents

struct DummyIntent: AppIntent {
    static var title: LocalizedStringResource = "dummy"
    func perform() async throws -> some IntentResult {

let phrase1: AppShortcutPhrase<DummyIntent> = "aaa"

From what I can tell, your DummyIntent seems to be conforming to the ExpressibleByStringLiteral protocol (though the docs on AppIntent don't seem to specify this anywhere, I assume there must be some code generation going on?).
To answer your questions:

  1. This is a language feature (since the beginning as far as I can tell). I've never heard of it having an official name but I've heard them called the ExpressibleBy*Literal or ExpressibleBy protocols. There's some great articles about them online like this one from NSHipster.
  2. There are ExpressibleBy protocols for almost all types of literals (integers, floating-point, arrays, dictionaries, etc.), and a type must conform to the appropriate protocol for this to work. (Your example is weird because I can't figure out how it conforms to ExpressibleByStringLiteral.)
  3. All you need to do to is conform to an ExpressibleBy protocol to make it work for that type of literal, like this:
struct MyString: ExpressibleByStringLiteral {
    // there's a couple different types this could be, not just `String`
    typealias StringLiteralType = String
    init(stringLiteral value: StringLiteralType) {
        // do something with `value`

let thing: MyString = "literal"

Thanks a lot @elijah-santos25 for answering my questions.

I was breaking my head over it as I didn't know how to search about this topic.

There is a video at What's New in Swift - WWDC19 - Videos - Apple Developer which talks about this search for "ExpressibleBy" in the transcript. I will watch it.

You can find some more information in the "Literals" section of The Swift Programing Language.


thanks a lot @1-877-547-7272 for the documentation link

1 Like