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-10135] Property with type 'Self' cannot override a property with type 'Self' #52537

Closed
swift-ci opened this issue Mar 20, 2019 · 7 comments
Closed
Assignees
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

swift-ci commented Mar 20, 2019

Previous ID SR-10135
Radar None
Original Reporter linqingmo (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Swift Development Snapshot 2019-03-17

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

md5: e8e72fa6406e42baa338fec2b7c90afe

Issue Description:

class A {
    required init() {}
    func copy() -> Self {
        let copy = Self.init()
        print(type(of: copy))
        return copy
    }
    
    var copied: Self {
        let copy = Self.init()
        return copy
    }
}


class B: A {
    override func copy() -> Self {
        let copy = super.copy() as! Self // supported
        return copy
    }
    
    override var copied: Self {
        let copy = super.copied as! Self // unsupported
        return copy
    }
}
@belkadan
Copy link
Contributor

Totally reasonable as long as it's a read-only property. I'm not quite going to tag this as a starter bug because I'm not sure what all the moving pieces would be, but I don't think it would be too hard.

@Moximillian
Copy link
Contributor

Maybe this is basic stuff, but...

if the subclass B had properties (e.g. var foo: Int = 0) and B().copy() instantiates parent A, which doesn't have foo property and only casts it to B, how come code doesn't crash on runtime when you do e.g. B().copy().foo = 5?

@swift-ci
Copy link
Collaborator Author

Comment by LinQingmo (JIRA)

@Moximillian It will not crash.

func copy() -> Self {
    let copy = Self.init() // When call B().copy(), this is let copy = B.init(), so it can cast to B.
    print(type(of: copy)) // When call B().copy(), the type of copy is B not A.
    return copy
}

@belkadan
Copy link
Contributor

Right, the required in the declaration of the init means it can be invoked on the dynamic type. If you just said A.init(), you'd get an error message about not returning the right type.

@Moximillian
Copy link
Contributor

Oh, inherited methods operate on instance of subclass, so is more similar to protocol extension, in a way... All makes sense now, thanks!

@johnno1962
Copy link
Contributor

PR filed to make declaring properties of type `Self` an error: #23485

@johnno1962
Copy link
Contributor

#23485 #23613

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