-8

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?

Huda Sajed
  • 17
  • 2
  • Yes, using several of the same techniques. – Kevin B Jan 22 '18 at 17:03
  • nice , but why downvote? – Huda Sajed Jan 22 '18 at 17:06
  • Wasn't mine, but the question you linked to was at -7 before it received an answer. – Kevin B Jan 22 '18 at 17:07
  • 4
    `extension Int { static func == (lhs: Int, rhs: Int) -> Bool { return true } }` ;) – Hamish Jan 22 '18 at 17:08
  • https://stackoverflow.com/questions/28848119/overloading-equivalence-operator-for-custom-class-in-swift looks like in swift you can use global functions to overload the `==` operator – Sterling Archer Jan 22 '18 at 17:12
  • yes it can be achievable by overloading `==` operator – Ali Faris Jan 22 '18 at 17:14
  • @Hamish Or `return [1, 2, 3].contains(rhs)` :) – Sweeper Jan 22 '18 at 17:14
  • Or even `func && (lhs: Bool, rhs: Bool) -> Bool { return true }` :D – Hamish Jan 22 '18 at 17:20
  • Or you could do https://gist.github.com/hamishknight/94652e3e8dfd9e92726810eb987892c2. Basically there are quite a few ways you could go about doing this; therefore IMO this question is too broad. – Hamish Jan 22 '18 at 17:26
  • Also you didn't show us the declaration of `a`, soooooo `var _a = 0; var a: Int { _a += 1; return _a }` :) – Hamish Jan 22 '18 at 17:33
  • Why is this so downvoted? It's a legitimate question. You should post your comments as an answer, @Hamish – Alexander Jan 22 '18 at 18:01
  • 1
    @Alexander I suspect people see it as an attempt to recreate the votes the JavaScript version got – Sterling Archer Jan 22 '18 at 18:30
  • @SterlingArcher So? If it's a valid question, who cares? – Alexander Jan 22 '18 at 18:30
  • @Hamish You would need to define a new `==` operator that takes `() -> Int` and `Int`, because your closure wouldn't be evaluated, otherwise – Alexander Jan 22 '18 at 18:31
  • @Alexander Nope, `a` is a computed variable, not a stored closure :) And while I'm not one of the downvoters, IMO this question is too broad and should be closed as such. – Hamish Jan 22 '18 at 18:35
  • @Hamish What makes it broad? The vast number of answers that exist? – Alexander Jan 22 '18 at 18:35
  • @Alexander Precisely – Hamish Jan 22 '18 at 18:36
  • @Hamish Ah, so that's pretty much what I did in my answer, hah – Alexander Jan 22 '18 at 18:36
  • @Hamish That's sketchy though. A person asking this question clearly wouldn't know that it has many answers, and that it would be closed for that. The question itself (in isolation from the number of anticipated answers) is well defined – Alexander Jan 22 '18 at 18:37
  • 1
    I don't find it too broad, off topic, or really fitting of any of the close reasons. but... i don't see it as being particularly useful, which is also why i downvoted the original. – Kevin B Jan 22 '18 at 19:21
  • @Alexander One only has to look at the original JS question to see the variety of answers that it has received; it's not unreasonable to assume that the same question for another language would be any different. Though that being said, the JS question was temporarily closed as being too broad before being re-opened (and staying that way, even after mods visiting), so it seems the mood of the community is that such questions aren't considered too broad. – Hamish Jan 22 '18 at 23:43
  • @Alexander Btw, my most horrific attempt at this [now looks like this](https://gist.github.com/hamishknight/6c7d7ccf2e6aacb4163fe8d72f74eade) :) – Hamish Jan 23 '18 at 00:25

2 Answers2

1

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
}
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
0

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
Alexander
  • 59,041
  • 12
  • 98
  • 151