1. 2

    In what other languages would it be possible?

    I guess everything with properties (functions disguised as fields) so D, C#, etc.

    Afaik not with C, C++, or Java.

    1. 26
      #define a (++i)
      int i = 0;
      
      if (a == 1 && a == 2 && a == 3)
          ....
      
      1. 1

        Isn’t that undefined behavior? Or is && a sequence point?

        1. 3

          && and || are sequence points. The right expression may never happen depending on the result of the left, so it would make things interesting if they weren’t.

      2. 10

        This is very easy to do in C++.

        1. 5

          You can also do it with Haskell.

          1. 3

            Doable with Java (override the equals method), and as an extension, with Clojure too:

            (deftype Anything []
              Object
              (equals [a b] true))
            
            (let [a (Anything.)]
              (when (and (= a 1) (= a 2) (= a 3))
                (println "Hello world!")))
            

            Try it!

            Or, inspired by @zge above:

            (let [== (fn [& _] true)
                  a 1]
              (and (== a 1) (== a 2) (== a 3)))
            
            1. 3

              Sort of. In Java, == doesn’t call the equals method, it just does a comparison for identity. So

               a.equals(1) && a.equals(2) && a.equals(3); 
              

              can be true, but never

               a == 1 && a == 2 && a == 3;
              
            2. 3

              perl can do it very simply

              my $i = 0;
              sub a {
              	return ++$i;
              }
              
              if (a == 1 && a == 2 && a == 3) {
              	print("true\n");
              }
              
              1. 2

                Here is a C# version.

                using System;
                
                namespace ContrivedExample
                {
                    public sealed class Miscreant
                    {
                        public static implicit operator Miscreant(int i) => new Miscreant();
                
                        public static bool operator ==(Miscreant left, Miscreant right) => true;
                
                        public static bool operator !=(Miscreant left, Miscreant right) => false;
                    }
                
                    internal static class Program
                    {
                        private static void Main(string[] args)
                        {
                            var a = new Miscreant();
                            bool broken = a == 1 && a == 2 && a == 3;
                            Console.WriteLine(broken);
                        }
                    }
                }
                
                1. 2

                  One of the ‘tricks’ where all a’s are different Unicode characters is possible with Python and Ruby. Probably in Golang too.

                  1. 7

                    In python, you can simply create class with __eq__ method and do whatever you want.

                    1. 4

                      Likewise in ruby, trivial to implement

                      a = Class.new do
                        def ==(*)
                          true
                        end
                      end.new
                      
                      a == 1 # => true
                      a == 2 # => true
                      a == 3 # => true
                      
                  2. 2

                    In Scheme you could either take the lazy route and do (note the invariance of the order or ammount of the operations):

                    (let ((= (lambda (a b) #t))
                           (a 1))
                      (if (or (= 1 a) (= 2 a) (= 3 a))
                          "take that Aristotle!"))
                    

                    Or be more creative, and say

                    (let ((= (lambda (x _) (or (map (lambda (n) (= x n)) '(1 2 3)))))
                            (a 1))
                        (if (or (= 1 a) (= 2 a) (= 3 a))
                            "take that Aristotle!"))
                    

                    if you would want = to only mean “is equal to one, two or three”, instead of everything is “everything is equal”, of course only within this let block. The same could also be done with eq?, obviously.

                    1. 1

                      Here is a Swift version that uses side effects in the definition of the == operator.

                      import Foundation
                      
                      internal final class Miscreant {
                          private var value = 0
                          public static func ==(lhs: Miscreant, rhs: Int) -> Bool {
                              lhs.value += 1
                              return lhs.value == rhs
                          }
                      }
                      
                      let a = Miscreant()
                      print(a == 1 && a == 2 && a == 3)
                      
                    1. 2

                      Can anyone give me some examples of the sort of things you write on a personal wiki? I’ve always enjoyed the idea of having one, but at the same time I can’t maintain one for a long time. I feel like it’s too much effort, and that I can find whatever I want using Google anyway.