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-1736] Super class method returning Self returns superclass type instead of Self #44345

Closed
swift-ci opened this issue Jun 12, 2016 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1736
Radar rdar://problem/35435303
Original Reporter hanjustin (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Xcode Version 9.1 (9B55)
Swift 4.0

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

md5: 5a7173bd581ca6612eb55c17a3d359c7

Issue Description:

Calling parent class's method with return type Self is expected to return type Self. However, calling the method using 'super.methodName' returns the parent class's type instead of Self.

class ParentWallet {
    var money: Int
    
    required init(money: Int) {
        self.money = money
    }
    
    func newWalletWithMoreMoney(_ moreMoney: Int) -> Self {
        let moneyForNewWallet = money + moreMoney
        return type(of:self).init(money: moneyForNewWallet )
    }
}

class ChildWallet: ParentWallet {
    required init(money: Int) {
        super.init(money: money)
    }
    
    override func newWalletWithMoreMoney(_ moreMoney: Int) -> Self {
        let newWallet = super.newWalletWithMoreMoney(moreMoney)    // newWallet has type ParentWallet instead of Self
        return newWallet    // Error: Cannot convert return expression of type 'ParentWallet' to return type 'Self'
    }
}

Without the override function in ChildWallet class, these are sample behaviors:

// wallet1 has type ParentWallet
let wallet1 = ParentWallet(money: 10).newWalletWithMoreMoney(5)
// wallet2 has type ChildWallet
let wallet2 = ChildWallet(money: 10).newWalletWithMoreMoney(5)
@belkadan
Copy link
Contributor

cc @jckarter

@slavapestov
Copy link
Member

I just hit this issue while testing a fix for another bug. I'll try to figure out a fix, it should be straightforward.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Nov 9, 2017

Comment by Junkai Wu (JIRA)

update: this is still a bug in Swift 4

@slavapestov
Copy link
Member

Yeah, sorry, I never got around to fixing it.

@slavapestov
Copy link
Member

@swift-ci create

@AnthonyLatsis
Copy link
Collaborator

#36251

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

No branches or pull requests

4 participants