How can I compare two generic objects. Below is sample code of doing a comparison and this line elem > value throws an error saying Could not find overload for '>' that accepts the supplied arguments
func index<T : Equatable>(array: T[], value: T) -> Int {
for (index, elem) in enumerate(array) {
if elem > value {
return index
}
}
return array.count
}
From the Swift Reference:
The Equatable protocol makes it possible to determine whether two values of the same type are considered to be equal.
There is one required operator overload defined in the protocol: ==.
There is no guarantee that Equatable objects have to implement the > operator, which explains your error.
Take a look at Comparable however. Notice that comparable only needs to overload the < and == operators.
However, if not a < b nor is a == b, you can assume that a > b.
You want Comparable, not Equatable. Equatable only has ==.
Related
class Test {
var count: Int;
init(count: Int) {
self.count = count;
}
}
extension Test: Comparable {
static func <(lhs: Test, rhs: Test) -> Bool {
return lhs.count > rhs.count
}
}
When I write this extension everything work okay but when i change < to > compiler error return
Type 'Test' does not conform to protocol 'Equatable'
Comparable extension required write < this function
What is this reason?
If you look at the docs for Comparable, you can see that it inherits from Equatable.
Equatable requires == to be implemented:
static func ==(lhs: Test, rhs: Test) -> Bool {
return lhs.count == rhs.count
}
I should also mention that count does not have an initial value. So you need to add an initializer for Test or a initial value to count.
EDIT:
If you look at the docs for Comparable, you'll find this bit:
Types with Comparable conformance implement the less-than operator (<)
and the equal-to operator (==). These two operations impose a strict
total order on the values of a type, in which exactly one of the
following must be true for any two values a and b:
a == b
a < b
b < a
So you must implement < and == but > is unnecessary. That's why it doesn't work when you only have >.
I'm looking to avoid writing unnecessary code. If I define == and < for a type, is it possible to automatically obtain <= (< or ==), > (not < and not ==), or >= (not <)? I would think Swift would provide these operators for free, but that doesn't seem to be the case.
If you make your type conform to Equatable, it will provide a default implementation of != based on your ==.
If you make your type conform to Comparable, it will provide default implementations of >, <=, and >= based on your <.
Note that Comparable extends Equatable, so you must also provide == to conform to Comparable.
Yes. That's exactly how Comparable protocol works. You only have to define 2 operators: < and ==. Swift will figure out the rest.
struct MyStruct : Comparable {
var value: Int
}
func == (lhs: MyStruct, rhs: MyStruct) -> Bool {
return lhs.value == rhs.value
}
func < (lhs: MyStruct, rhs: MyStruct) -> Bool {
return lhs.value < rhs.value
}
let a = MyStruct(value: 1)
let b = MyStruct(value: 42)
print(b > a) // We never defined `>` operator
Yes.
Implement == from Equatable and < from Comparable, and the rest of the operators will use the default implementations as you expect.
To add Comparable conformance to your custom types, define the < and == operators as static methods of your types. The == operator is a requirement of the Equatable protocol, which Comparable extends—see that protocol’s documentation for more information about equality in Swift. Because default implementations of the remainder of the relational operators are provided by the standard library, you’ll be able to use !=, >, <=, and >= with instances of your type without any further code.
Suppose I want to add up all the values of an entry of an array. Not only integers, but also double values or some type I created myself which implements the + operator. So my question is: Is it possible to create such a function with a constraint that is based on the fact if the type implements the operator? Such as or something like that (obviously THIS isn't working).
Thanks in advance
There are several ways you can approach the problem.
IntegerArithmeticType
While I know of no explicit way to say "Hey Swift, I want to only allow type T where T has this operator", all types we commonly think of as capable of being added, subtracted, multiplied, and divided automatically conform to IntegerArithmeticType, which can be used as a generic constraint for any generic function or type.
For example:
func addAll<T: IntegerArithmeticType>(array: [T]) -> T {
var count = array[0]
for (index, value) in array.enumerate() {
if index != 0 {
count += value
}
}
return count
}
Notice in this quick mock-up version I initialized count with the first value of the array and then avoiding double-counting by checking the index against 0 inside the for loop. I can't initialize count with 0 because 0 is an Int while I want count to be of type T.
You mentioned having your own types work with this too. One option is to have your custom type conform to IntegerArithmeticType, although it requires a lot more than just the + operator. For details on IntegerArithmeticType, see SwiftDoc.
Custom Protocol
If conforming to IntegerArithmeticType imposes some sort of limitation on you, you can create a custom protocol with the + function and whatever other requirements you would like. After all, operators are really just functions. Note that you can add conformance to already existing types with extensions. For example:
protocol Addable {
func +(left: Self, right: Self) -> Self
// Other requirements...
}
extension Int: Addable {}
extension Double: Addable {}
Say I have two variables, and I don't know what type they are (and it's not possible to know what type they are until runtime):
var a: Any
var b: Any
How can I test if they are equal, using the Equatable protocol? I can't just do a == b because that requires that both of the items are the same, Equatable type, and the compiler can't prove that because they could be different types (and one or both might not even be Equatable).
So, is it possible to tell the compiler to check if they both have the same type, and if that type conforms to Equatable, then to use the == operator on them and return the result, otherwise returning false?
If there is no way to do this, is there a good reason Swift prevents this, or is it a current limitation of Swift that could be fixed in the future?
Under the assumption that if a and b differ in type then they are never equal, you can use a generic function with constraint to achieve the goal.
func isEqual<T : Equatable>(a: T, b: T) -> Bool {
return a == b;
}
You cannot have a and b differ in type, as the Equatable protocol assumes that the LHS and RHS of the comparison are of the same type. This seems a reasonable constraint, but one can certainly write a notion of equality that doesn't require this. In these cases, you'll need your own equality protocol.
You use generics and function overload:
func isEqual<T: Equatable>(a: T, b: T) -> Bool {
return a == b
}
func isEqual<T, U>(a: T, b: U) -> Bool {
return false
}
If both variables have the same type, as that type conforms to Equatable, then the compiler will choose the first function, otherwise will go the second one.
This will work for Objective-C objects too, providing you cast to NSObject before calling the function: isEqual(var1 as? NSObject, var2 as? NSObject)
I'm writing a recursive descent parser. I'd like my parser to work on any (or at least "many") Collections of UInt8 (e.g., not only Swift.Array)
func unpack<T: CollectionType where T.Generator.Element == UInt8>(t: T) {
let m = t.dropFirst()
//[do actual parsing here]
unpack(m)
}
However:
error: cannot invoke 'unpack' with an argument list of type '(T.SubSequence)'
note: expected an argument list of type '(T)'
This is puzzling because:
dropFirst returns Self.SubSequence
CollectionType.SubSequence is SubSequence : Indexable, SequenceType = Slice<Self>
Slice is CollectionType.
Therefore, m should be CollectionType.
However for some reason, this doesn't work. How do I define unpack so it can be recursively passed subsequences?
There is no CollectionType in Swift anymore. Both Array and ArraySlice adopt Sequence. And the dropFirst() method which you use is declared in Sequence. So you can make recursive generic function like this:
func unpack<T: Sequence>(t: T) where T.Element == UInt8 {
let m = t.dropFirst()
//[do actual parsing here]
unpack(m)
}