0

I'm new in swift programming language and my background was .NET Development, I wonder is there any protocol (interface) or abstract class similar to C#'s IEqualityComparer in swift language?

Note: I'm trying to developing SDK (I already had written it in C# and trying to converting into Swift) that has an interface that you can use it with in generic type. Interface has a method that using IEqualityComparer.

Best regards.

bkulaksiz
  • 77
  • 1
  • 9
  • 1
    You should explain more about your situation: Why do you need this? The best way to do this in Swift could be different depending on what you are using this for. – Sweeper Jun 12 '20 at 13:02
  • @Sweeper I'm trying to developing SDK (I already had written it in C# and trying to converting into Swift) that has an interface that you can use it with in generic type. Interface has a method that using IEqualityComparer. – bkulaksiz Jun 12 '20 at 13:19
  • @bkulaksiz Remember that languages differ more than their particular spelling of the same set of elements. While C# and Swift *are* relatively similar, the general approach of "take every element in program written in X, find its closest counterpart in Y" almost always ends up with terrible Y code. – Alexander Jun 12 '20 at 13:29
  • 1
    `IEqualityComparer` is just like Java's `Comparator`. See this answer: https://stackoverflow.com/a/45742746/3141234 – Alexander Jun 12 '20 at 13:31
  • Generic interfaces generally don't translate very well into Swift. While `IEqualityComparer` could be translated into a closure type `(T, T) -> Bool` (given you don't need the hash code), I'm not sure about the generic interface in your SDK. I would suggest you redesign the whole thing. – Sweeper Jun 12 '20 at 13:31
  • @Alexander-ReinstateMonica "is just like" is probably too strong a wording. It's just like `Comparator`, _but for equality only_. – Sweeper Jun 12 '20 at 13:33
  • @Sweeper Not just equality, but also hashing, so depending on the SDK needs, I would suggest `Equatable` or `Hashable` protocols as the closest equivalent. – NetMage Jun 12 '20 at 19:54
  • @Sweeper Yes, I misspoke. The specific similarities I wanted to point out were: 1) `IEqualityComparer` is an interface that's implemented by a separate comparator object, not the comparable object itself 2) its equivalent in Swift is just a closure of type `(T, T) -> Bool`. – Alexander Jun 12 '20 at 19:56
  • @Alexander-ReinstateMonica It is just a closure but unfortunately it seems like closure support isn't as universal in Swift - i.e. there is no `Dictionary` that takes a closure for equality. – NetMage Jun 12 '20 at 20:17
  • @NetMage [Sure there is.](https://developer.apple.com/documentation/swift/dictionary/2430767) – Alexander Jun 12 '20 at 21:20
  • @Alexander-ReinstateMonica That isn't a `Dictionary` . That is the operator defined by `Equatable` protocol, which as you pointed out, isn't the same as a random closure passed into a `Dictionary` for use as a key comparison operator. – NetMage Jun 12 '20 at 21:25
  • @NetMage Oh I see what you mean. That's done in a different way: you make a wrapper struct which wraps your objects and conforms to `Equatable` using whatever special meaning of equality that you want. I think that's a better design, because you don't have to pass a comparator in every context that might want to do equating. It's just self-contained within the value. – Alexander Jun 12 '20 at 21:41
  • @NetMage Here's an example that uses a wrapper object to make a `Set` that hashes/equates objects by their identity, instead of their natural value-based hashing/equality: https://stackoverflow.com/a/57763847/3141234 – Alexander Jun 12 '20 at 21:46
  • @Alexander-ReinstateMonica That isn't a Swift Library class. Swift Library doesn't seem to have a native `Dictionary` that supports user specified equality the way C# does. – NetMage Jun 12 '20 at 21:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215848/discussion-between-alexander-reinstate-monica-and-netmage). – Alexander Jun 12 '20 at 22:10

1 Answers1

0

That would be Equatable protocol:

https://developer.apple.com/documentation/swift/equatable

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • It's different. It's the same as the difference between Java's `Comparable` and `Comparator`. – Alexander Jun 12 '20 at 13:31
  • @Alexander-ReinstateMonica Yes, I think strictly speaking it is equivalent to `Hashable` protocol. – NetMage Jun 12 '20 at 19:56
  • @NetMage Not quite. `Hashable` is implemented by the thing being hashed. Whereas `IEqualityComparer` isn't implemented by `Foo` necessarily (although it can be). It's implemented by separate comparer objects, which can define different ways of checking equalities between two `Foo` objects. – Alexander Jun 12 '20 at 19:58
  • @Alexander-ReinstateMonica I see what you mean. `Hashable` protocol is like `IEquatable` and not `IEqualityComparer`. – NetMage Jun 12 '20 at 20:03
  • @NetMage I don't see a hashcode requirement on the interface, but yeah, you get the point – Alexander Jun 12 '20 at 21:18