fjgandrade / sharpkit

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

Indexer operator with out parameters #280

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is a simplified version of some code implementing splines.

        [JsType(JsMode.Clr, Filename = "res/Default.js")]
        public class Point
        {
            public double X { get; set; }
            public double Y { get; set; }
        }

        [JsType(JsMode.Clr, Filename = "res/Default.js")]
        public class Spline
        {
            private readonly IList<Point> _points;

            public Spline(IList<Point> points)
            {
                _points = points;
            }

            internal bool InterpolateForPoint(double index, out Point point)
            {
                //Interpolation code elided
                point = _points[(int)Math.Floor(index)];
                return true;
            }

            public Point this[double index]
            {
                get
                {
                    Point res;
                    bool foundIt = InterpolateForPoint(index, out res);
                    if (foundIt)
                    {
                        return res;
                    }
                    return new Point();
                }
            }

        }

        static void DefaultClient_Load()
        {
            new jQuery(HtmlContext.document.body).append("Ready<br/>");
        }

        static void btnTest_click(DOMEvent e)
        {
            new jQuery(HtmlContext.document.body).append("Hello world<br/>");

            Spline spline = new Spline(new List<Point> { new Point { X = 1.2, Y = 3.4 }, new Point { X = 3.3, Y = 2.4 } });

            Point point;

            new jQuery(HtmlContext.document.body).append("Val: " + spline.InterpolateForPoint(1.5, out point)); //This works

            new jQuery(HtmlContext.document.body).append("Val: " + spline[1.5]); //This throws

        }

The call via the indexer throws an exception. The cause of the problem seems to 
be the handling of 'this'.

        InterpolateForPoint: function (index, point)
        {
            point.Value = this._points.get_Item$$Int32(Cast(System.Math.Floor$$Double(index), System.Int32.ctor));
            return true;
        },
        Item$$: "SharpKitWebApp4.DefaultClient+Point",
        get_Item$$Double: function (index)
        {
            var res;
            var foundIt = (function ()
            {
                res = {Value: res};
                var $res = this.InterpolateForPoint(index, res);
                res = res.Value;
                return $res;
            })();
            if (foundIt)
            {
                return res;
            }
            return new SharpKitWebApp4.DefaultClient.Point.ctor();
        }

/*Generated by SharpKit 5 v5.00.5000*/

Is this a limitation of using the features together?

Original issue reported on code.google.com by co...@gravill.com on 13 Feb 2013 at 2:35

GoogleCodeExporter commented 9 years ago

Original comment by DanelK...@gmail.com on 21 Jun 2013 at 4:57