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-10106] Move execution of default parameters to external context. #52508

Closed
swift-ci opened this issue Mar 14, 2019 · 2 comments
Closed

[SR-10106] Move execution of default parameters to external context. #52508

swift-ci opened this issue Mar 14, 2019 · 2 comments
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10106
Radar None
Original Reporter k-o-d-e-n (JIRA User)
Type Improvement
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: 9783f89900de35561fa484416cd4b302

duplicates:

  • SR-6913 Default arguments are not evaluated correctly in larger expression

Issue Description:

By creating struct to obtain source code context information, I expected that default parameter in my custom error initialiser will be execute in external context, like similar default parameters #file, #line, #function. But that didn't.

struct SourceCodeContext {
    let file: String
    let line: Int
    let function: String

    init(_ file: String = #file, _ line: Int = #line, _ function: String = #function) {
        self.file = file
        self.line = line
        self.function = function
    }
}

struct SomeError: Error {
    let context: SourceCodeContext

    init(_ context: SourceCodeContext = SourceCodeContext()) {
        self.context = context
    }
}

let error = SomeError()
error.context.line /// expected 25, but has initializer line value - 21
error.context.function /// `init` value

Environment: Xcode 10.1 (10B61)

@belkadan
Copy link
Contributor

I think this is correct behavior; otherwise you wouldn't be able to find where the SourceCodeContext initializer was called. But it might be worth bringing up on the forums.

@belkadan
Copy link
Contributor

Looks like SR-6913 covers both this case and a more explicit version.

@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
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

2 participants