I saw this JavaScript question, and I'm wondering if it is possible to do the same in Swift.
Is this possible?
if a==1 && a==2 && a==3 {
print("amazing")
}
If so, then how?
I saw this JavaScript question, and I'm wondering if it is possible to do the same in Swift.
Is this possible?
if a==1 && a==2 && a==3 {
print("amazing")
}
If so, then how?
you can overload ==
operator for Int
type
extension Int {
static func == (lhs: Int, rhs: Int) -> Bool
{
print("== overloading")
return rhs > 0 && rhs < 4
}
}
let a = 1;
if a == 1 && a == 2 && a == 3 {
print("PASS"); //always will pass
}else{
print("FAIL"); //will never execute
}
Here's one example, that doesn't involve sneaky redefinitions of ==
and &&
:
struct Counter {
var count = 0
mutating func increment() { count += 1 }
var a: Int {
mutating get {
increment()
return count
}
}
mutating func sneakyFunction() {
if a==1 && a==2 && a==3 {
print("amazing");
}
}
}
var c = Counter()
c.sneakyFunction() // => amazing