feat(minor): use Self keyword in order to support types with generics#35
Conversation
Summary of ChangesHello, 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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| 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 | ||
| } | ||
| } |
There was a problem hiding this comment.
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
)
supersonicbyte
left a comment
There was a problem hiding this comment.
Thank you for your contribution! LGTM
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?
Minimal checklist:
DocCcode-level documentation for any public interfaces exported by the package