Skip to content

[Feature] Support Covariant Self #109

Open
@berbs-fetch

Description

@berbs-fetch

Use Case

Support covariant Self used in methods and members with the @Mocked annotation.
Since our Mocks are final and Self is a key word this should be supportable? We detect the usage of Self and replace with new class name instead of Self

Feature Proposal

I would like @Mocked to support covariant Self out of the box. Currently this isn't allowed.

@Mocked protocol Test {
    func newInstance() -> Self
}

The code generated is:

@MockedMembers
final class TestMock: Test {
    func newInstance() -> Self // compiler error of "Covariant `Self` or `Self?` can only appear at the top level of property type.
}

Instead I would like this code generated:

@MockedMembers
final class TestMock: Test {
    func newInstance() -> TestMock
}

This is actually the code Xcode recommends to get around the issue.

Alternatives Considered

Manually building the Mock class using @MockedMembers. It works but is undesirable.

Additional Context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions