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-11398] Self type does not behave correctly in subclasses of a protcol implementation #53799

Closed
swift-ci opened this issue Aug 30, 2019 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11398
Radar None
Original Reporter ezamagni (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 11.0 beta 6 (11M392r)

Apple Swift version 5.1 (swiftlang-1100.0.270.6 clang-1100.0.32.1)

macOS 10.14.6 (18G95)

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

md5: 7bbc923a93acf7925ca4d3fc49375d04

duplicates:

  • SR-103 Protocol Extension: function's implementation cannot be overridden by a subclass

Issue Description:

Note: please open the attached code sample and have a look at the README


The following code defines a simple SomeProtocol with a static var definition svar.
A default implementation of svar is provided via extension, along with an instance method that prints its value via Self.

protocol SomeProtocol {
    static var svar: String { get }
    func printStaticVar()
}

extension SomeProtocol {
    static var svar: String {
        return "Default svar"
    }
    func printStaticVar() {
        print(Self.svar)
    }
}

Considering the following class hierarchy of protocol adoption class:

class BaseImplementation: SomeProtocol {
    
}

class ChildImplementation: BaseImplementation {
    class var svar: String {
        return "ChildImplementation's svar"
    }
}

we have an inconsistency if we try to execute the following code:

let childImpl = ChildImplementation()
childImpl.printStaticVar()      // output: Default svar
print(type(of: childImpl).svar) // output: ChildImplementation's svar

According to the official Self type documentation the two outputs should be the same:

Writing Self.someStaticMember to access a member of the current type is the same as writing type(of: self).someStaticMember

A more thorough explanation of the wrong behavior is provided in the attached sample project.

@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

1 participant