views:

1492

answers:

5

How can polymorphism replace an if-else statement or Switch inside of a loop? In particular can it always replace an if-else? Most of the if-thens I use inside of loops are arithmetic comparisons. This question is spawned from this question.

int x;
int y;
int z;

while (x > y)
{
     if (x < z)
     {
         x = z;
     }
}

How would this work with polymorphism?
NOTE: I wrote this in Java but am interested in this for any OOL.

+1  A: 

Polymorphism is not really applicable in the example you provided.

See this SO answer.

Mitch Wheat
If it was Smalltalk rather that java, the example would already be polymorphic.
Pete Kirkham
...but it's not Smalltalk!
Mitch Wheat
The question is language agnostic. A direct translation of the code into smalltalk would be polymorphic.
Pete Kirkham
+4  A: 

Polymorphism usually replaces switch statements when each case corresponds to a different type. So instead of having:

public class Operator
{
    string operation;

    public int Execute(int x, int y)
    {
         switch(operation)
         {
             case "Add":
                 return x + y;
             case "Subtract":
                 return x - y;
             case "Multiply":
                 return x * y;
             case "Divide":
                 return x / y;
             default:
                 throw new InvalidOperationException("Unsupported operation");
         }
    }
}

you'd have:

public abstract class Operator
{
    public abstract int Execute(int x, int y)
}

public class Add : Operator
{
    public override int Execute(int x, int y)
    {
        return x + y;
    }
}

// etc

However, for the comparison type of decision you provided, polymorphism really doesn't help.

Jon Skeet
+1  A: 

Polymorphism can only replace if tests when the if test is basically dispatchi g to a variety of methods depending on the "type" of an object. Eg if object is type X invoke foo if it's a Y invoke bar and so. In this contrived example one would define an interface DoSonething with a method bad(). Both X and Y would implement Baz and have their respective baz() invoke foo() for X and bar() for Y. This simply calling baz() would eliminate the need for a if test.

mP
+1  A: 

In Smalltalk, "if" is actually a polymorphic method in Boolean. In the following example:

[ x>y ] whileTrue:  
  [   
    ( x<z ) ifTrue: [ x:=z ]        
  ]

The ifTrue:aBlock message is implemented in True as "execute this block" and in False as "ignore this block", so depending on what the (x<z) evaluates to, either implementation will be called.

So in Smalltalk polymorphism replaces every if-else construct by default :)

Rafał Dowgird
Disclosure: I have never used smalltalk. Is there not still a Boolean value here? If so is it just not a fancy IF-Then-Else statement wrapped in polymorphism? I'm just asking for clarification.
WolfmanDragon
Yes, there is a Boolean value and it is an if-then-else statement implemented via polymorphism. I am aware that this polymorphism happens at a different level than the one from the question, but I think that the mechanism is still interesting enough to justify this answer.
Rafał Dowgird
+1  A: 

One pattern is to have objects which represent the result of the test, and objects which represent the block to execute. The result objects have overridden selection functions, so if Bool had a choose(T positive, T negative) then Bool.TRUE would return the positive argument and Bool.FALSE would return the negative. Naive implementations of smalltalk-family languages work like that.

To encode your while loop in that form, there needs to call the choose method on the result of comparing x and y to determine whether to call the block for the inside of the while loop, and that block also uses compare and choose to set the value of x. A more literal translation would be to choose either a block which sets x to z or one which does nothing; instead it just uses choose to set x back to the same value.

Obviously it's overkill and inefficient for this simple case.

public class WantonPolymorphism {

    static class Int32 {
        final int value;
        Int32 ( final int value ) { this.value = value; }

        Compare compare ( Int32 other ) {
            // Java runs out of turtles at this point unless you use
            // an enum for every value
            if ( this.value < other.value ) return Compare.LESS;
            if ( this.value > other.value ) return Compare.GREATER;
            return Compare.EQUAL;
        }
    }

    enum Compare {
        LESS {
            <T> T choose (T less, T equal, T greater) { return less; }
        },
        EQUAL {
            <T> T choose (T less, T equal, T greater) { return equal; }
        },
        GREATER {
            <T> T choose (T less, T equal, T greater) { return greater; }
        };

        abstract <T> T choose (T less, T equal, T greater) ;
    }

    interface Block { Block execute () ; }


    /**
     * Main entry point for application.
     * @param args The command line arguments.
     */
    public static void main (String...args) {
        Block block =  new Block() {
            Int32 x = new Int32(4);
            Int32 y = new Int32(3);
            Int32 z = new Int32(2);

            public Block execute () {
                System.out.printf("x = %d, y = %d, z = %d\n", x.value, y.value, z.value);

                return x.compare(y).choose(done, done, new Block () {
                    public Block execute () {
                        x = x.compare(z).choose(x,x,z);

                        return x.compare(y).choose(done, done, this);
                    }
                });
            }

            Block done = new Block () {
                public Block execute () {
                    System.out.printf("x = %d, y = %d, z = %d\n", x.value, y.value, z.value);
                    System.exit(0);
                    return this;
                }
            };
        };

        for(;;) 
            block = block.execute();
    }
}
Pete Kirkham
+1 for the concept. There is still if statements though.
WolfmanDragon