Preferring local privates over other extensions' privates in SE-0169

I'm having a little bit of a problem with SE-0169 currently.

The change is good in my opinion but it has a little nitpick/flaw in it. It makes it impossible to create declarations with the same name in different extensions.

extension MyType {
    private func doWork() {
        print("1")
    }

    func foo() {
        doWork() // Expected: 1
    }
}

extension MyType {
    private func doWork() {
        print("2")
    }

    func bar() {
        doWork() // Expected: 2
    }
}

Trying to compile this gives me unambiguity errors. The same is also true for types.

I know that the solution/workaround is separating it to different files or giving each one different names. But if I have a lot them, thats a lot of tedious work or coding violations (doWork1, doWork2, doWork3...).

I'm not the proposal's author but should we have an amendment to SE-0169 which makes local privates have more priority over other extensions' privates?

You are effectively trying to do this:

struct MyType {
    private func doWork() {
        print("1")
    }

    func foo() {
        doWork() // Expected: 1
    }

    private func doWork() { // invalid redeclaration
        print("2")
    }

    func bar() {
        doWork() // Expected: 2
    }
}

I believe this is a good thing. :slight_smile:

Well I said this because I wast trying to shorten the name of a protocol like this and ran across this issue.

extension SyntaxProtocol {
    private typealias `Protocol` = any SomeVeryLongProtocolName

    func asProtocol(_: `Protocol`.Protocol) -> `Protocol`? {
        Syntax(self).asProtocol((any SyntaxProtocol).self) as? `Protocol`
    }
}

extension SyntaxProtocol {
    private typealias `Protocol` = any AnyOtherVeryLongProtocolName

    func asProtocol(_: `Protocol`.Protocol) -> `Protocol`? {
        Syntax(self).asProtocol((any SyntaxProtocol).self) as? `Protocol`
    }
}

Well, maybe I should've only said nitpick because this might've been the only use case ever. :sweat_smile:

Just make your type aliases fileprivate, or move them to the top level of the source file.

2 Likes

I made a few mistakes on the example code, my bad. I updated it now.

Mean't to ping, sorry. ^

Uh oh, it looks like my example can't even compile on it own. I think had a LOT of tunnel vision while thinking about this, didn't I. Oh my god.

You do not need to reply to this thread anymore, thank you.

1 Like