Allow Equtable to be used in other files especially in Test files

The problem
In unit tests I want to compare my two objects if they are equal, but for that I have to mark my struct with Equatable protocol in the main app target which in fact is not requited in the main app.

I can't do

extension MyModel: Equatable {}

in test target as compiler gives the error Extension outside of file declaring struct 'MyModel' prevents automatic synthesis of '==' for protocol 'Equatable'

The solution

Allow test files to declare the Equatable for main target models

I tested out the 3000 models with and without Equatable.

The average compile time taken by main file without Equatable is 5.5s
The average compile time taken by main file with Equatable is 20.82
So our main target can save lot of build time.

The size of the main binary without Equatable is 7,698,801 bytes
The size of the main binary without Equatable is 10,142,833 bytes
On an average 1000 models takes 1 MB of binary size.

The code I used to generate models is below, just moved all printed models in main file and run the test

let numberOfModels = 3000

for i in 1...numberOfModels {
    print("struct My\(i)Model: Equatable {")
    print(" let isEditable: Bool")
    print(" let integer: Int")
    print(" let double: Double")
    print(" let string: String")
    print(" let float: Float")
    print("}\n")
}
1 Like

If you're willing to pay the unit test time price of implicit opening of existentials (any Equatable) and reflection (Mirror), you could do even without the conformance. Inspired by your post, I opened a PR to pointfreeco/swift-custom-dump providing just that.

I think we're looking at generalising this conformance synthesis using "conformance macros" (not yet pitched, but seems to be coming soon I think?).

I expect Equatable synthesis will be modified to use that general system, which may include some way for external modules to ask for a synthesised implementation.

1 Like

If it was just one test class then may be don't bother but for CI this might hit the CPU

Hey Karl where did you hear about conformance macros?

The macros vision document mentions them, as well as the newly accepted SE-0389.

1 Like