Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Multiple Equatable methods on different Packages – Bad practise

by fragi
May 24, 2023
in Project Mess
Share on FacebookShare on Twitter

In this article we will explore what happens when we have different equatable methods for a type across packages and why this is a bad practise.

A. Multiple Equatable methods in same file:

Lets start with the following example:

Swift
1
2
3
4
5
6
7
8
9
10
11
import UIKit
 
struct A: Equatable {
    var i: Int
    var j: Int
}
 
let lhs = A(i: 1, j: 2)
let rhs = A(i: 2, j: 2)
 
lhs == rhs

As we expect lhs == rhs is false.

Lets remove now the Equatabl and add the == function.

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import UIKit
 
func ==(lhs: A, rhs: A) -> Bool {
    lhs.j == rhs.j
}
 
struct A {
    var i: Int
    var j: Int
}
 
let lhs = A(i: 1, j: 2)
let rhs = A(i: 2, j: 2)
 
lhs == rhs

As we expect the lhs == rhs is true.

Lets now add back the Equatable and observe what will happen:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import UIKit
 
func ==(lhs: A, rhs: A) -> Bool {
    lhs.j == rhs.j
}
 
struct A: Equatable {
    var i: Int
    var j: Int
}
 
let lhs = A(i: 1, j: 2)
let rhs = A(i: 2, j: 2)
 
lhs == rhs

As we notice lhs == rhs is true, which means the == function takes priority.

B. Multiple Equatable methods across different packages

Lets now see what happens if  we create a project and add a swift package (let’s call it MyLibrary).
Add the following code there:

Swift
1
2
3
4
5
6
7
8
9
public struct A: Equatable {
    var i: Int
    var j: Int
    
    public init(i: Int, j: Int) {
        self.i = i
        self.j = j
    }
}

On the main target let’s add now the following extension

Swift
1
2
3
4
5
extension A {
    static public func ==(lhs: A, rhs: A) -> Bool {
        lhs.j == rhs.j
    }
}

And lets a test in the main target:

Swift
1
2
3
let lhs = A(i: 1, j: 2)
let rhs = A(i: 2, j: 2)
XCTAssertEqual(lhs, rhs)

The test fails. The priority goes to the default equatable method that created automatically on the “MyLibrary” module.

fragi

fragi

Related Posts

Project Mess #3: Hardcoded UI colours
Project Mess

Project Mess #3: Hardcoded UI colours

February 19, 2022

In this short article we will see another project mess.Let's see the following example: A worst variation of this...

Project Mess #2 – Magic numbers for UI spacing
Project Mess

Project Mess #2 – Magic numbers for UI spacing

February 18, 2022

In this short article we will address one common issue that can lead to a project mess is when all...

Project Mess #1: No Localised Strings.
Project Mess

Project Mess #1: No Localised Strings.

May 13, 2021

Every app has some static text. Either as a title of a screen or as a title of a button,...

Next Post

Unit tests in playground

Equal arrays

C Union equivalent in Swift

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.