Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-12034] ExpressibleByStringLiteral initializer called unexpectedly #54471

Closed
ZevEisenberg opened this issue Jan 15, 2020 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@ZevEisenberg
Copy link
Contributor

Previous ID SR-12034
Radar None
Original Reporter @ZevEisenberg
Type Bug
Status Closed
Resolution Invalid
Environment

Xcode 11.3 (11C29) on macOS 10.15.2 (19C57)

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: e59adf842cff91a91c3807f16c38aeda

duplicates:

  • SR-10259 ExpressibleByStringLiteral masks an initializer without any warning in Swift 5.

Issue Description:

I've got this type conformed to ExpressibleByStringLiteral, but I don't want that initializer to be invoked if I initialize the type directly via Example("foo"). (In the real code this came from, I want to capture #file and #line at the initialization site, and that's not happening because the wrong initializer is being selected.)

struct Example {
    let value: String


    init(_ value: String) {
        print(#function) // this does not print
        self.value = value
    }


}

extension Example: ExpressibleByStringLiteral {
    init(stringLiteral value: String) {
        print(#function) // this prints
        self.value = value
    }
}


let x = Example("foo")
@ZevEisenberg
Copy link
Contributor Author

Thanks to @sharplet for pointing out that this is expected behavior, and is a result of SE-0213. Adam shared a couple of workarounds, including Example.init("foo") and Example("foo" as String). Posting them here in case they help someone else who is surprised by this behavior. In my case, I'm only using the protocol as a band-aid to help me get through a big refactor, so I'll be removing it before merging anyway.

@beccadax
Copy link
Contributor

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

2 participants