Announcement

Collapse
No announcement yet.

Bridge.box/unbox

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

  • Bridge.box/unbox

    There is common case when you need to create object and pass it to external API.
    We used the following way:
    public class Program
    {
        public static void Main()
        {
            var w = 100;
            var h = 200;
            var o = new object
            {
                ["width"] = w,
                ["height"] = h
            };
        }
    }
    Now that doesn't work. Because w and h are boxed:
    Bridge.define("Demo.Program", {
            main: function Main() {
                var w = 100;
                var h = 200;
                var o = function (_o1) {
                        _o1.width = Bridge.box(w, System.Int32);
                        _o1.height = Bridge.box(h, System.Int32);
                        return _o1;
                    }(Bridge.unbox({  }));
            }
        });
    What is the best way to construct such objects now, so that values are not boxed ?

  • #2
    One way is to use Cast, but that seems very weird, doesn't it ?
    public class Program
    {
        public static void Main()
        {
            var w = 100;
            var h = 200;
            var o = new object
            {
                ["width"] = w.Cast<int>(),
                ["height"] = h.Cast<int>()
            };
        }
    }

    Comment


    • #3
      rumc,

      I think the best option would be to use [External] ObjectLiteral - it creates "simple" objects and provides strong typing.
      Dynamic ("simple" object as well) and anonymous types (a "wrapper" class created) can be usefull as well.

      https://deck.net/60caa42af21768336b295d82aa3a55dc

      public class Program
      {
          public static void Main()
          {
              var w = 100;
              var h = 200;
      
              // Dynamic
              dynamic o = new object();
              o.width = w;
              o.height = h;    
      
              // ObjectLiteral
              var l = new Data
              {
                  width = w,
                  height = h
              };
      
              // Anonymous
              var a = new 
              {
                  width = w,
                  height = h
              };
          }
      }
      
      [ObjectLiteral]
      [External]
      class Data
      {
          public int width;
          public int height;
      }
      The sample above generates the following JavaScript:
      /**
       * @compiler Bridge.NET 16.0.0-beta
       */
      Bridge.assembly("Demo", function ($asm, globals) {
          "use strict";
      
          Bridge.define("Demo.Program", {
              main: function Main() {
                  var w = 100;
                  var h = 200;
      
                  // Dynamic
                  var o = {  };
                  o.width = w;
                  o.height = h;
      
                  // ObjectLiteral
                  var l = { width: w, height: h };
      
                  // Anonymous
                  var a = new $asm.$AnonymousType$1(w, h);
              }
          });
      
          Bridge.define("$AnonymousType$1", $asm, {
              $kind: "anonymous",
              ctors: {
                  ctor: function (width, height) {
                      this.width = width;
                      this.height = height;
                  }
              },
              methods: {
                  equals: function (o) {
                      if (!Bridge.is(o, $asm.$AnonymousType$1)) {
                          return false;
                      }
                      return Bridge.equals(this.width, o.width) && Bridge.equals(this.height, o.height);
                  },
                  getHashCode: function () {
                      var h = Bridge.addHash([7550196186, this.width, this.height]);
                      return h;
                  },
                  toJSON: function () {
                      return {
                          width : this.width,
                          height : this.height
                      };
                  }
              }
          });
      });

      Comment


      • #4
        Also looks strange
        var o = new object();
        o["blabla"] = 1.As<int>();

        Comment


        • #5
          Vladimir, where can I read about the rules of boxing and unboxing used in bridge ?

          Comment


          • #6
            rumc , any feedback on https://forums.bridge.net/forum/brid...=4220#post4220 ?


            Vladimir, where can I read about the rules of boxing and unboxing used in bridge ?
            The same rules as in .Net.

            Comment


            • #7
              Hi,

              You can use dynamic object to avoid boxing
              https://deck.net/809d0540352b23619f02ac71bb5291af

              Comment


              • #8
                Vladimir, I think introducing boxing to Bridge leads to project isolation and complication.
                Without boxing it was simple to maintain public API libraries. Libraries built on Bridge and used by other external JavaScript projects.
                Now you always have to think very carefully about pulic fields in the project, because it may happen that some values inside the objects are boxed, and external pure-JS projects will have troubles reading the values.

                Perhapse a good way will be to make a configuration parameter "boxing", which could be set to false.
                Last edited by rumc; 2017-05-19 @ 01:26 PM.

                Comment


                • #9
                  The goal of Bridge to reproduce .NET behavior as much as possible. You compile C# code and get the same result as in .NET application.
                  If you want to avoid boxing then there are few workarounds (like dynamic, object literal classes or templates (https://deck.net/2fe8102a101b4ea71d54892f2c2a242e)).

                  But default behavior will be the same as in .NET (at least we try to implement in such way).

                  Comment


                  • #10
                    Got your point.
                    Vladimir, am I right that boxing/unboxing is only used for numeric types ?

                    Comment

                    Working...
                    X