Binary Operator '+' Cannot Be Applied to Two 'T' Operands

Binary operator '+' cannot be applied to two 'T' operands

Swift doesn't know that the generic type T has a '+' operator. You can't use + on any type: e.g. on two view controllers + doesn't make too much sense

You can use protocol conformance to let swift know some things about your type!

I had a go in a playground and this is probably what you are looking for :)

protocol Addable {
func +(lhs: Self, rhs: Self) -> Self
}

func add<T: Addable>(num1: T, _ num2: T) -> T {
return num1 + num2
}

extension Int: Addable {}
extension Double: Addable {}
extension Float: Addable {}

add(3, 0.2)

Let me know if you need any of the concepts demonstrated here explained

binary operator ' ' cannot be applied two T operands

You need to tell Swift that T conforms to Comparable protocol in order for it to allow using operator < on objects of type T:

func maxNumer<T : Comparable>(array:[T]) -> T {
// ^^^^^^^^^^
var maxNumer = array[0]
for var i = 0; i < array.count-1; i++ {
if maxNumer < array[i+1] { //This line gets error as title
maxNumer = array[i+1]
}
}
return maxNumer
}

Binary operator '==' cannot be applied to two operands

Update: SE-0143 Conditional conformances has been implemented in Swift 4.2.

As a consequence, your code does compile now. And if you define Item as a struct

struct Item: Equatable {
let item: [[Modifications: String]]

init(item: [[Modifications: String]]) {
self.item = item
}
}

then the compiler synthesizes the == operator automatically,
compare SE-0185 Synthesizing Equatable and Hashable conformance


(Pre Swift 4.1 answer:)

The problem is that even if == is defined for the dictionary type
[Modifications: String], that type does not conform to
Equatable. Therefore the array comparison operator

public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool

cannot be applied to [[Modifications: String]].

A possible concise implementation of == for Item would be

func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.item.count == rhs.item.count
&& !zip(lhs.item, rhs.item).contains {$0 != $1 }
}

Your code compiles for [[String: String]] – if the Foundation
framework is imported, as @user3441734 correctly said – because then [String: String] is automatically converted to NSDictionary which conforms to
Equatable. Here is a "proof" for that claim:

func foo<T : Equatable>(obj :[T]) {
print(obj.dynamicType)
}

// This does not compile:
foo( [[Modifications: String]]() )

// This compiles, and the output is "Array<NSDictionary>":
foo( [[String: String]]() )

Binary operator '/' cannot be applied to two 'T' operands

Simply specify one of the Numeric protocols.

For floating point precision:

struct PickerRange<T:FloatingPoint> {
var start: T
var end: T
var step: T

var length: T {
return max(start, end) - (min(start,end) / step)
}
}

SignedInteger, UnsignedInteger or other Numeric protocols are also an option.

// Comparable

Neither of these require of you to specify Comparable additionally, so specifying one of these is suffice for the case.

EDIT:

How to make one structure for Int and Double?

Essentially, you can, by specifying SignedNumeric:

struct PickerRange<T:SignedNumeric & Comparable> {
var start: T
var end: T
var step: T
}

// Treated as PickerRange<Double>
let a = PickerRange(start: 1.0, end: 5.0, step: 1)

// Treated as PickerRange<Int>
let b = PickerRange(start: 1, end: 5, step: 1)

However, you have specialized length property, which includes / operator not supported by SignedNumeric. A workaround would consist in type-checking/force-casting to a type you have initialized your struct with:

struct PickerRange<T:SignedNumeric & Comparable> {
var start: T
var end: T
var step: T

var length: T {
if T.self is Double.Type {
return (max(start, end) as! Double) - ((min(start, end) as! Double) / (step as! Double)) as! T
} else if T.self is Int.Type {
return (max(start, end) as! Int) - ((min(start, end) as! Int) / (step as! Int)) as! T
} else {
return 0
}
}
}

let a = PickerRange(start: 1.0, end: 5.0, step: 1)
let b = PickerRange(start: 1, end: 10, step: 1)

print(a.length)
print(b.length)

Swift: binary operator ' ' cannot be applied to two 'Comparable' operands

The < operator applies to two values of a single type that conforms to Comparable — not to two things designated merely as Comparable. A protocol does not conform to itself.

The use of any with MyElement doesn't affect this basic fact about the Comparable-constrained generic placeholder T. Since MyElement is generic over T, we would have to know that the two different MyElement-conforming objects you propose to sort resolve their T to the same type as one another — and in the code you wrote, we don't know that, so the compiler balks.

Binary operator '~=' cannot be applied to two 'Optional Error ' operands

Here, the word error actually refers to the parameter error, rather than a new variable:

case .ExportError(error):

So you are essentially saying:

// code for illustration purposes only, not supposed to work
(error as? ClassError) == .some(.ExportError(error))

Note that the two occurrences of error refers to the same thing. That's probably not what you meant, but the compiler thinks that is what you meant, and tries to pattern match the associated value of ExportError against error using the ~= operator, as error here is considered an expression pattern

However, both operands are of type Optional<Error>. The ~= operator on Optional<Wrapped> requires that Wrapped conforms to Equatable, but Error does not conform to Equatable as it is a protocol. Hence the error message.

You probably meant to introduce a new identifier, so you should use a binding pattern, rather than just an expression pattern.

case .ExportError(let exportError):
print("Error :", exportError)

Binary operator '==' cannot be applied to two 'Equatable' operands... what?

Replace Element == Equatable with Element: Equatable.

getting error: Binary operator '==' cannot be applied to two 'x' operands, how to remove certain number of elements in this array of objects

== is defined on Equatable, which your Cards type is not. You either need to make Cards conform to equatable, and decide which of your properties counts towards "equal" (The same type? the same income? both? What about the images?), or compare directly on the properties you do care about.

Error comparision - Binary operator == cannot be applied two Error operands

You need to cast the Swift.Error to your custom ServiceError. The common approach is to use a switch:

enum ServiceError: Error {
case serverDown, userNotExist
}


let error: Error? = ServiceError.serverDown as Error

switch error as? ServiceError {
case .serverDown: print("Server is down") // "Server is down\n"
case .userNotExist: print("User not found")
case .none: print("error:", error ?? "nil")
}

Note: It is Swift naming convention to name your enumeration cases starting with a lowercase letter.



Related Topics



Leave a reply



Submit