fjgandrade / sharpkit

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

Extensions using Func currying do not work. #307

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Introduce this code:
using SharpKit.JavaScript;
using SharpKit.Html;
using SharpKit.jQuery;
using System;

namespace MyNamespace
{
    public static class Extensions {
        public static Func<TParam, TResult> Wrap<TParam, TResult>(this Func<object[], TResult> func)
        {
            return (arg) =>
            {
                return func(new object[] { arg });
            };
        }
    }

    [JsType(JsMode.Global)]
    public class MyPageClient : HtmlContext
    {
        public static void MyMethod()
        {
            var generic = new Generic<Implementer, string>(new Implementer());

            new jQuery(document.body).append(generic.Make<int>(10)(100));
        }    
    }    

    class Generic<T, TResult>
        where T:Interface<TResult> {

        readonly T inner;
        public Generic(T inner) { this.inner = inner; }
        public Func<TParam, TResult> Make<TParam>(int n) {
            return this.inner.MakeFunc(n).Wrap<TParam, TResult>();
        }
    }

    class Implementer : Interface<string> {
        public Func<object[], string> MakeFunc(int n) {
            return (object[] parameters) => {
                string msg = "";
                foreach(object obj in parameters) {
                    msg += n + " " + obj.ToString();
                }

                return msg;
            };
        }
    }

    interface Interface<T> {
        Func<object[], T> MakeFunc(int n);    
    }
}

What is the expected output? What do you see instead?
Expected output is... good question, I do not know.

But the actual output is:
/*Generated by SharpKit 5 v5.00.5000*/
function MyMethod()
{
    var generic = new MyNamespace.Generic$2.ctor(MyNamespace.Implementer.ctor, System.String.ctor, new MyNamespace.Implementer.ctor());
    $(document.body).append(generic.Make$1(System.Int32.ctor, 10)(100));
};

I do not see how that output implements any of the previous code.

What version of the product are you using? On what operating system?
5.5

Please provide any additional information below.
Do not claim to  'translate any C# expression to JavaScript' when you CLEARLY 
DO NOT.

Original issue reported on code.google.com by ing.marc...@gmail.com on 1 Aug 2013 at 2:58

GoogleCodeExporter commented 9 years ago
Nevermind... I added the attributes and it does compile. You guys are fucking 
awesome.

/*Generated by SharpKit 5 v5.00.5000*/
if (typeof ($CreateAnonymousDelegate) == 'undefined') {
    var $CreateAnonymousDelegate = function (target, func) {
        if (target == null || func == null) return func;
        var delegate = function () {
            return func.apply(target, arguments);
        };
        delegate.func = func;
        delegate.target = target;
        delegate.isDelegate = true;
        return delegate;
    }
}

function Wrap(func) {
    return function (arg) {
        return func([arg]);
    };
};

function MyMethod() {
    var generic = new MyNamespace.Generic$2.ctor(MyNamespace.Implementer.ctor, System.String.ctor, new MyNamespace.Implementer.ctor());
    $(document.body).append(generic.Make$1(System.Int32.ctor, 10)(100));
};
if (typeof (JsTypes) == "undefined") var JsTypes = [];
var MyNamespace$Generic$2 = {
    fullname: "MyNamespace.Generic$2",
    baseTypeName: "System.Object",
    assemblyName: "2bf0c6617dba4750a6689ebc8bc5239b",
    Kind: "Class",
    definition: {
        ctor: function (T, TResult, inner) {
            this.T = T;
            this.TResult = TResult;
            this.inner = null;
            System.Object.ctor.call(this);
            this.inner = inner;
        },
        Make$1: function (TParam, n) {
            return Wrap(this.inner.MakeFunc(n));
        }
    }
};
JsTypes.push(MyNamespace$Generic$2);
var MyNamespace$Implementer = {
    fullname: "MyNamespace.Implementer",
    baseTypeName: "System.Object",
    assemblyName: "2bf0c6617dba4750a6689ebc8bc5239b",
    interfaceNames: ["MyNamespace.Interface$1"],
    Kind: "Class",
    definition: {
        ctor: function () {
            System.Object.ctor.call(this);
        },
        MakeFunc: function (n) {
            return $CreateAnonymousDelegate(this, function (parameters) {
                var msg = "";
                for (var $i2 = 0, $l2 = parameters.length, obj = parameters[$i2]; $i2 < $l2; $i2++, obj = parameters[$i2]) {
                    msg += n + " " + obj.toString();
                }
                return msg;
            });
        }
    }
};
JsTypes.push(MyNamespace$Implementer);
var MyNamespace$Interface$1 = {
    fullname: "MyNamespace.Interface$1",
    baseTypeName: "System.Object",
    assemblyName: "2bf0c6617dba4750a6689ebc8bc5239b",
    Kind: "Interface"
};
JsTypes.push(MyNamespace$Interface$1);

Original comment by ing.marc...@gmail.com on 1 Aug 2013 at 3:08

GoogleCodeExporter commented 9 years ago
Great :-), closing this issue...

Original comment by DanelK...@gmail.com on 1 Aug 2013 at 7:50