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-7986] Error when using similarly named local function when initializing a static property #50519

Closed
nevil opened this issue Jun 13, 2018 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@nevil
Copy link

nevil commented Jun 13, 2018

Previous ID SR-7986
Radar None
Original Reporter @nevil
Type Bug
Status Resolved
Resolution Duplicate
Environment

macOS High Sierra
swift-4.2-DEVELOPMENT-SNAPSHOT-2018-06-12-a
swift-DEVELOPMENT-SNAPSHOT-2018-06-08-a
Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

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

md5: f53d720ba08240ee4553203c391fabd2

duplicates:

  • SR-1772 File-level function with the same name as instance function not picked up by compiler

Issue Description:

Compiling the source below results in an error:

import Foundation

func afunction(_ pattern: String, options: [String]? = nil) -> String {
    return pattern
}


class Testing {
    private static let defaultValue = afunction("Foo", options: ["Bar"])

    private func afunction(for file: String, using pattern: String) -> String {
        return "Hogehoge"
    }

    init() {}
}


Testing()
global-vs-local.swift:9:39: error: cannot use instance member 'afunction' within property initializer; property initializers run before 'self' is available
    private static let defaultValue = afunction("Foo", options: ["Bar"])
                                      ^
global-vs-local.swift:19:1: warning: result of 'Testing' initializer is unused
Testing()
^      ~~

The compiler seem to assume that the local instance function, afunction(for:using), is intended when actually the global afunction(:options:) is meant.
I assume that the labels should be enough to understand which function is intended.

@belkadan
Copy link
Contributor

This is by design, but the design's long been controversial. Meanwhile, you can explicitly use MyModule.afunction(…) syntax to disambiguate.

@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