Skip to content

feat(minor): use Self keyword in order to support types with generics#35

Merged
supersonicbyte merged 1 commit intoordo-one:mainfrom
ThePreppy:generate-conformance-for-types-with-generics
Mar 24, 2026
Merged

feat(minor): use Self keyword in order to support types with generics#35
supersonicbyte merged 1 commit intoordo-one:mainfrom
ThePreppy:generate-conformance-for-types-with-generics

Conversation

@ThePreppy
Copy link
Copy Markdown
Contributor

Description

This PR adds support for types with generics in their signature. Instead of using a particular type in the signature of == function, use Self which works for types that contain generics.

How Has This Been Tested?

  • Unit tests are passing
  • An additional unit test has been added to cover the new scenario

Minimal checklist:

  • I have performed a self-review of my own code
  • I have added DocC code-level documentation for any public interfaces exported by the package
  • I have added unit and/or integration tests that prove my fix is effective or that my feature works

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the Equatable macro's implementation to enhance its compatibility with Swift's type system, particularly for generic types. By leveraging the Self keyword in the generated == operator signatures, the macro can now correctly synthesize equality conformance for structures and classes that utilize generic parameters, thereby expanding its utility and robustness.

Highlights

  • Generic Type Support: The Equatable macro now correctly supports types with generics by using the Self keyword in the generated == function signatures.
  • Macro Code Generation Update: The macro's code generation logic has been updated to emit lhs: Self, rhs: Self instead of lhs: <TypeName>, rhs: <TypeName> for the == operator.
  • New Test Case for Generics: An additional unit test has been introduced to specifically verify the macro's behavior and correctness when applied to generic types.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the EquatableMacro to use Self instead of the concrete type name in the generated == operator signature for Equatable conformance, improving code consistency. It also introduces a new generic struct TestGenericContent and a corresponding test case to ensure the macro works correctly with generic types. A suggestion was made to refactor the test suite by introducing a helper function to reduce duplication in generating expected conformance strings, which would improve maintainability.

Comment on lines 67 to 89
case .nonisolated:
"""
extension Person: Equatable {
nonisolated public static func == (lhs: Person, rhs: Person) -> Bool {
nonisolated public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id && lhs.name == rhs.name && lhs.lastName == rhs.lastName && lhs.random == rhs.random
}
}
"""
case .isolated:
"""
extension Person: Equatable {
public static func == (lhs: Person, rhs: Person) -> Bool {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id && lhs.name == rhs.name && lhs.lastName == rhs.lastName && lhs.random == rhs.random
}
}
"""
case .main:
"""
extension Person: @MainActor Equatable {
public static func == (lhs: Person, rhs: Person) -> Bool {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id && lhs.name == rhs.name && lhs.lastName == rhs.lastName && lhs.random == rhs.random
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a lot of duplication across the tests for generating the expected conformance string. This block of code is repeated in many tests with only minor changes. To improve maintainability and reduce boilerplate, consider creating a helper function that generates this string based on the type name, properties to compare, and isolation level. This would make the tests cleaner and easier to manage.

For example, you could have a helper like this:

private func makeExpectedConformance(
    for typeName: String,
    comparing properties: [String],
    isolation: Isolation
) -> String {
    let comparisons = properties.map { "lhs.\($0) == rhs.\($0)" }.joined(separator: " && ")
    let body = comparisons.isEmpty ? "true" : comparisons

    switch isolation {
    case .nonisolated:
        return """
        extension \(typeName): Equatable {
            nonisolated public static func == (lhs: Self, rhs: Self) -> Bool {
                \(body)
            }
        }
        """
    case .isolated:
        return """
        extension \(typeName): Equatable {
            public static func == (lhs: Self, rhs: Self) -> Bool {
                \(body)
            }
        }
        """
    case .main:
        return """
        extension \(typeName): @MainActor Equatable {
            public static func == (lhs: Self, rhs: Self) -> Bool {
                \(body)
            }
        }
        """
    }
}

Then you could use it in your test like this:

let generatedConformance = makeExpectedConformance(
    for: "Person",
    comparing: ["id", "name", "lastName", "random"],
    isolation: isolation
)

Copy link
Copy Markdown
Collaborator

@supersonicbyte supersonicbyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! LGTM

@supersonicbyte supersonicbyte merged commit 8f5c009 into ordo-one:main Mar 24, 2026
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants