Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare "Any" value types

Tags:

swift

I have several "Any" value types that I want to compare.

var any1: Any = 1 var any2: Any = 1  var any3: Any = "test" var any4: Any = "test"  print(any1 == any2) print(any2 == any3) print(any3 == any4) 

Using the == operator shows an error:

"Binary operator '==' cannot be applied to two 'Any' (aka 'protocol<>') operands"

What would be the way to do this ?

like image 468
the Reverend Avatar asked Jan 13 '16 23:01

the Reverend


People also ask

Can you compare two different data types in C?

As a general rule, C will not compare two values if they are not the same type and will never implicitly convert a variable to a type with less precision.

What is equality comparison?

Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they're considered equal.

How do you compare data types in Java?

In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.

How can you test whether two values evaluate to the same result and are both of the same type?

Equality and Inequality Operators. The == and === operators check whether two values are the same, using two different definitions of sameness. Both operators accept operands of any type, and both return true if their operands are the same and false if they are different.

How many types of value are there?

We categorized the items into separate types of value and ended up with five that seem to describe most cases quite well. We’ll discuss these in no particular order below. Note: because our personal experience lies mostly with commercial organizations, most of our examples are from there as well.

What are the different types of valuation methods?

What are the Main Valuation Methods? When valuing a company as a going concern, there are three main valuation methods used by industry practitioners: (1) DCF analysis, (2) comparable company analysis, and (3) precedent transactions. These are the most common methods of valuation used in investment banking.

What is the difference between a variable and a data type?

A variable on the other hand is an abstraction of a data storage element and can hold values. A flip-flop is a good example of a storage element. Verilog data-type reg can be used to model hardware registers since it can hold values between assignments.

What is comparable company analysis (comparable comps)?

Comparable company analysis (also called “trading multiples” or “peer group analysis” or “equity comps” or “public market multiples”) is a relative valuation method in which you compare the current value of a business to other similar businesses by looking at trading multiples like P/E, EV/EBITDA, or other ratios.


1 Answers

The only way to do this is with a function other than == that takes a type parameter, and then compares the values if they are both of that type:

func isEqual<T: Equatable>(type: T.Type, a: Any, b: Any) -> Bool {     guard let a = a as? T, let b = b as? T else { return false }      return a == b } 

Now, using your variables above, you can compare them like this:

var any1: Any = 1 var any2: Any = 1  var any3: Any = "test" var any4: Any = "test"  isEqual(type: Int.self, a: any1, b: any2)      // true isEqual(type: Int.self, a: any2, b: any3)      // false isEqual(type: String.self, a: any3, b: any4)   // true 
like image 62
Aaron Rasmussen Avatar answered Sep 28 '22 18:09

Aaron Rasmussen