Announcement

Collapse
No announcement yet.

typescript Function type to c# definition

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    typescript Function type to c# definition

    Hello, i am trying to make a typescript definition to c# conversion program,
    i see that in typescript you can have things like that:
    export class PropertyBinding {
            constructor(rootNode: any, path: string, parsedPath?: any);
    
            path: string;
            parsedPath: any;
            node: any;
            rootNode: any;
    
            getValue(targetArray: any, offset: number): any;
            setValue(sourceArray: any, offset: number): void;
            bind(): void;
            unbind(): void;
    
            BindingType: { [bindingType: string]: number };
            Versioning: { [versioning: string]: number };
    
            GetterByBindingType: Function[];
            SetterByBindingTypeAndVersioning: Array<Function[]>;
    
            static create(root: any, path: any, parsedPath?: any): PropertyBinding|PropertyBinding.Composite;
            static parseTrackName(trackName: string): any;
            static findNode(root: any, nodeName: string): any;
        }
    here you got some builtin typescript type called "Function" used,
    so i'd like to traduce it too with my program, but i dont know how i could do,
    C# just dont have it at all.

    problem is,
    even if i use c# "object", we cant pass methods to it, without casting it to an Action or a Func first.

    do you have any idea how i could handle it? So when we write code that use the c# definition we could at least see that it is a Func<> or Action<> wrapper class.

    I mean a way to use a thing like that:

        public class FunctionWrapper
            {
                object self;
                public  Action<T> ToAction<T>()
                {
                    return ((Action<T>)self);
                }
                public Action<T, U> ToAction<T, U>()
                {
                    return ((Action<T, U>)self);
                }
                // etc...
                public Func<T> ToFunction<T>()
                {
                    return ((Func<T>)self);
                }
                //etc...
                public void SetAction(Action<T> action)
                {
                    self = (object)action;
                }
                //etc...
                public void SetFunction(Function<T> function)
                {
                    self = (object)function;
                }
            }
    with that:

    [ExternalAttribute]
    public class Test
    {
        public static FunctionWrapper wrapper;
    }
    and that:
    MyNamespace.Test.wrapper.SetFunction<float, string>(myFunction);
    but in javascript it would give:
    MyNamespace.Test.wrapper = myFunction;
    Thanks by advance for any tips/advices.
    Last edited by axolotl; 2016-10-02 @ 01:46 AM.

    #2
    I'm not sure if i understand you correctly, but you want on js/ts side to know whether passed function is Action or Func ?
    Can you provide a use case what you want to achieve ?

    Comment


      #3
      For instance, i am trying to procedurally translate threejs typescript definition to c# to use it with bridge.net

      as i stated, in typescript there is a builtin type "Function" that i found in my typescript definition of threejs

      I want to find a way to translate "Function" wich is like a callback but without any type constraints to put it in the c# definition that i want to generate.

      edit:
      if it is a callback and i put "object" as type for it in the generated c#,
      its hard to understand we are talking about a callback.

      edit2:
      in a typescript definition file i want to convert to use with bridge...
      someClassField: Function;
      in the c# i ll generate if i got no other solution...
      public object someClassField;
      edit3:

      totally another problem but:
      this will give me issues to traduce too:
      (its typescritpt and i want it c#)
      genericVariable: <u, t>(parm1: t) => t;
      for now i got a Func<T, T> from it, but i cant use the <u, t> generic parameters in it.

      edit4:

      also, i saw there was builtins defined in Bridge.Net.Html5 (ArrayBuffer, ArrayBufferView, etc...)
      Is there any Bridge.Net Version of that builtin: "ArrayLike<number>"? (i found it too in threejs typescript definition.)
      Last edited by axolotl; 2016-10-04 @ 01:24 PM.

      Comment


        #4
        Hello,

        I don't know the answer to the question (at least, off the top of my head), but I just remembered there was an attempt of Three.js Bridge bindings. Here are related links. It is just for your information.

        Forum thread:
        http://forums.bridge.net/forum/gener...for-bridge-net

        GitHub:
        https://github.com/guidovanhilst/SharpThreejs

        Live Demo:
        http://creativecadtechnology.com/threejssharp/

        Comment


          #5
          If you are translating typescript to c#, you don't need to keep exact syntax, since you will have free hand how to write them in c# (unless you want to call something else that is written in typescript). If you want to call specific function Func or delegate should do the trick for you.

          Can you provide example how whould you want to make a call in c# ? And what is your usecase here? Will your c# will be calling some external JS?

          If some external code will call your function as callback, and you want this implementation in c# it need to be strong typed (you can use Any<> if different types of arguments can be passed to this callback). Dynamic arguments you can implement in multiple ways, using object params[] or dynamic, or even get "arguments" using Script.Write().
          Implementation could be like this :

          public delegate void Callback(params Any<int,string>[] args);
          
          public Callback myCallback;

          Comment


            #6
            Originally posted by Daniil View Post
            Hello,

            I don't know the answer to the question (at least, off the top of my head), but I just remembered there was an attempt of Three.js Bridge bindings. Here are related links. It is just for your information.

            Forum thread:
            http://forums.bridge.net/forum/gener...for-bridge-net

            GitHub:
            https://github.com/guidovanhilst/SharpThreejs

            Live Demo:
            http://creativecadtechnology.com/threejssharp/
            Thank you, but thats why i am making a better one, that one is not complete at all, lot and lot of stuff is missing.

            So i try to make something to convert any typescript definition to c# definition.

            i have a better definition for threejs yet:
            https://drive.google.com/open?id=0B4...mJlNGQyckFhQ1E

            but i dont have time to test it and make some examples like the guy did, maybe some enums arent correctly written, or things like that, i dont know yet, but so i prefer to spend my time on testing and improving the conversion tool i made.

            @kcudnik

            thank you very much, i didnt know delegates could have dynamic return type and parameter types.

            the solution i choosed:
            public delegate dynamic Function(params dynamic[] someParams);
            but so we ll have to write the callbacks that way and only that way, thats a bit sad.



            Also,
            working with threejs, i see a lot of "ArrayLike" generic type use, should i convert them to simple arrays?

            ArrayLike<number> become float[] ?


            And again that question stay:

            genericVariable: <u, t>(parm1: t) => t;
            how could i turn it to c#?

            delegates takes generic parameters:
            public delegate v DelegateWithGenericParameters<u, v>(v param0);
            works in c#,

            but when it come to use it as a type, you are forced to describe the generic parameters:
            public class SomeClass
            {
             // you are forced to speficify the type used.
             public DelegateWithGenericParameters<string, int> myCallBack;
            // when in typescript, before i traduce it, it would look better like that:
             public DelegateWithGenericParameters myCallBack;
            // but that last thing generate a compilation error, saying it needs type arguments.
            }
            Last edited by axolotl; 2016-10-09 @ 04:44 PM.

            Comment


              #7
              Wow this is nice demo, i still think you can use strong type functions instead of dynamic delegates, just add a type and use it, it should not be a problem, just when you then need other type just add anothe one to Any<>. dynamic are good feature but you can cut yourself with them since you can write anything. If you need to do some actual havy js stuff that will be hard to express in C# just encapsulate that method to strong type c# and you are done.

              Comment


                #8
                thank you ^^. and again, any idea about lambdas with generic parameters?

                e.g.:
                export class smthg
                {
                    public someVar : <T, U, V>(someparameters : any[]) => void
                }
                edit:
                is there any way i could add some methods that are not on js side, specify it, and when the user call it, its the c# method of the class called instead of the javascript one? (so i could trick with some things maybe)
                Last edited by axolotl; 2016-10-12 @ 05:05 PM.

                Comment

                Working...
                X