Start a new topic

Problem foreach with switch and linq

Strange problem with a switch in a for loop with linq / lambda expression.

The generated code for the following dummy method is not correct, the switch will break out the parent for loop.

   

public class X
        {
            public int A = 10;
            public int B = 20;
        }

        public decimal TestForLoopSwitchFail(CalculationContext context)
        {
            var result = 0m;
            var x = new List<int>() { 10, 14 };

            var y = new List<X>() { new X(), new X { A = 22 } };
            foreach (var type in x)
            {
                var initial = y.Sum(e => e.A);

                var production = 0;
                switch (type)
                {
                    case 1:
                        production = 12;
                        break;
                    case 2:
                        production = 14;
                        break;
                    default:
                        production = 0;
                        break;
                }

                if(production > 0)
                    result += 20 + initial;
            }

            return result;
        }

   

The result in the generated .js code


$p.TestForLoopSwitchFail = function Calculation_TestForLoopSwitchFail(context) {
        var result = 0;
        var x = (function() {
            var $obj = new (System.Collections.Generic.List$1(System.Int32).ctor)();
            $obj.Add(10);
            $obj.Add(14);
            return $obj;
        }).call(this);
        var y = (function() {
            var $obj = new (System.Collections.Generic.List$1(A.B.C.D.E.F.Calculation.X).ctor)();
            $obj.Add(new A.B.C.D.E.F.Calculation.X.ctor());
            $obj.Add((function() {
                var $obj = new A.B.C.D.E.F.Calculation.X.ctor();
                $obj.A = 22;
                return $obj;
            }).call(this));
            return $obj;
        }).call(this);
        for (var $i = 0, $length = x.storage.length; $i != $length; $i++) {
            var $loopResult = (function() {
                var type = x.storage[$i];
                var initial = System.Linq.Enumerable.Sum$1(A.B.C.D.E.F.Calculation.X, 
                    y, $d.delegate(function(e) {
                        return e.A;
                    }, this));
                var production = 0;
                switch (type) {
                    case 1:
                        production = 12;
                        return { type: 3, label: null, depth: 0 };
                    case 2:
                        production = 14;
                        return { type: 3, label: null, depth: 0 };
                    default:
                        production = 0;
                        return { type: 3, label: null, depth: 0 };
                }
                if (production > 0)
                    result += 20 + initial;
                return { type: 0 };
            }).call(this);
            if ($loopResult.type == 3)
                break;
        }
        return result;
    };

 The switch contains return statements instead of a break, because of the return the code after the switch will not be reached.

This only happens if there is a statement like this "y.Sum(e => e.A)" before the switch.


Thanks for reporting we will investigate it and try to fix it in the coming version.


Is there any progress on this issue?

DuoCode 1.2 already released last week, but this issue will be fixed in version 1.3.

For now as a workaround only you can change switch to if..else... statements.

Thanks again

Hello Ruben,


Just wanted to let you know that we fixed this issue.

You can download a hot fixed DuoCode 1.3 from:

http://we.tl/SATCeagXDr


Thanks again for reporting,

Daniel


1 person likes this
Login or Signup to post a comment