fireway1026 / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

Repeated enumeration of IEnumerable #165

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It looks like the enumerable can only be enumerated once in the javascript 
generated

static void btnTest_click(HtmlDomEventArgs e)
{
    J(document.body).append("Hello world<br/>");

    var someNumbers = new List<int> { 2, 7, 5, 6 };
    var bigNumbers = someNumbers.Select(n => n > 5);

    J(document.body).append(bigNumbers.Count().ToString());
    J(document.body).append(bigNumbers.Count().ToString());
}

/*Generated by SharpKit 5 v4.28.4000*/
function btnTest_click(e)
{
    $(document.body).append("Hello world<br/>");
    var someNumbers=(function()
    {
        var $v1=new System.Collections.Generic.List$1.ctor(System.Int32);
        $v1.Add(2);
        $v1.Add(7);
        $v1.Add(5);
        $v1.Add(6);
        return $v1;
    }).call(this);
    var bigNumbers=System.Linq.Enumerable.Select$2$$IEnumerable$1$$Func$2(System.Int32,System.Boolean,someNumbers,function(n)
    {
        return n > 5;
    });
    $(document.body).append(System.Linq.Enumerable.Count$1$$IEnumerable$1(System.Boolean,bigNumbers).toString());
    $(document.body).append(System.Linq.Enumerable.Count$1$$IEnumerable$1(System.Boolean,bigNumbers).toString());
};

I expect the count (2) to be written to the page twice but instead I get 

'GetEnumerator': object is null or undefined.

on the second call.

I checked with a console application

static void Main(string[] args)
{
    var someNumbers = new List<int> { 2, 7, 5, 6 };
    var bigNumbers = someNumbers.Where(n => n > 5);

    Console.WriteLine(bigNumbers.Count().ToString());
    Console.WriteLine(bigNumbers.Count().ToString());
    Console.WriteLine(bigNumbers.First());
    Console.WriteLine(bigNumbers.First());
}

which prints out 2 2 7 7

Is it possible to support multiple iteration?

Original issue reported on code.google.com by co...@gravill.com on 10 Jul 2012 at 10:35

GoogleCodeExporter commented 8 years ago
Hi, thanks for the report, there is no reason why multiple enumerations will 
not be supported, I'll check it out. 
You can look at the implementation in the JsClr project if you want... 
Thanks!

Original comment by DanelK...@gmail.com on 10 Jul 2012 at 10:41

GoogleCodeExporter commented 8 years ago

Original comment by DanelK...@gmail.com on 12 Jul 2012 at 2:48