Announcement

Collapse
No announcement yet.

C# Tuple vs. JavaScript array?

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

    C# Tuple vs. JavaScript array?

    I would like to return a Tuple<T1, T2,...> from a method (instead of object[], for type safety) and access to Item1, Item2, etc. fields would translate into [0], [1], etc. in JavaScript? Would this require a change in Bridge, or can it be done in my code?

    #2
    Please post a DotNetFiddle sample of the C# that you would like.

    https://dotnetfiddle.net/

    And another simple sample of what you require in JavaScript would be helpful.

    Comment


      #3
      
      using Bridge;
      using System;
      
      namespace Sample
      {
          [External]
          public class External
          {
              public static Tuple<string, bool, int> GetThings()
              {
                  return Tuple.Create("One", true, 3);
              }
          }
      
          public class Main
          {
              public static void Run()
              {
                  var things = External.GetThings();
                  string one = things.Item1;
                  bool two = things.Item2;
                  int three = things.Item3;
              }
          }
      }
      Generates:

      
      (function (globals) {
          "use strict";
      
          Bridge.define('Sample.Main', {
              statics: {
                  run: function () {
                      var things = Sample.External.getThings();
                      var one = things.item1;
                      var two = things.item2;
                      var three = things.item3;
                  }
              }
          });
      
          Bridge.init();
      })(this);
      Desired:

      
      (function (globals) {
          "use strict";
      
          Bridge.define('Sample.Main', {
              statics: {
                  run: function () {
                      var things = Sample.External.getThings();
                      var one = things[0]; // <--- index into array
                      var two = things[1]; // <--- index into array
                      var three = things[2]; // <--- index into array
                  }
              }
          });
         
          Bridge.init();
      })(this);
      Basically, I want "Tuple<string, bool, int>" to be represented by a JavaScript array. As it stands now, using Tuple with [External] is rather useless.

      Comment


        #4
        Hi

        You can implement own Tuple class. Also, if you mark class like External then you should not implement any method inside such class (I mean External.GetThings)
        See the sample
        http://live.bridge.net/#bcf9189b8284...8e5545be598a29

        Comment


          #5
          Here is a concrete example where this would be extremely helpful.

          https://github.com/Reactive-Extensio...erators/zip.md

          The best I can do right now, is to return object[], and then type-cast each item to the type:

              [External]
              public static class Observable
              {
                  public static Observable<object[]> Zip<T1, T2>(Observable<T1> o1, Observable<T2> o2) { return null; }
              }
              ...
              Observable.Zip(boolObservable, intObservable).Subscribe(result =>
              {
                  bool r1 = (bool) result[0];
                  int r2 = (int) result[1];
              })
          This is the type-safe haven:

              [External]
              public static class Observable
              {
                  public static Observable<Tuple<T1, T2>> Zip<T1, T2>(Observable<T1> o1, Observable<T2> o2) { return null; }
              }
              ...
              Observable.Zip(boolObservable, intObservable).Subscribe(result =>
              {
                  bool r1 = result.Item1;
                  int r2 = result.Item2;
              })
          Basically, Tuple becomes a type-safe array in C#, backed by a regular array in JavaScript.

          Comment


            #6
            Vladimir -- thanks for the suggestions. The method body was for demo purpose only. I guess, I could implement my own Tuple, but that would be cumbersome, since Tuple is built into the BCL and is treated specially. I'd have to do a lot of custom re-implementation of everything that Tuple has to offer out of the box. My point is that Tuple in External classes should be treated as a JavaScript array, not as a type with "item1", "item2", "item3" fields, because that's not very useful. Instead, Bridge should output "[0]", "[1]", "[2]"...

            Comment


              #7
              Tuples are commonly used in four ways:
              1. To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
              2. To provide easy access to, and manipulation of, a data set.
              3. To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
              4. To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.

              Comment

              Working...
              X