Uploaded image for project: 'CloverDX'
  1. CloverDX
  2. CLO-18916

Allow automatic upcasting in CTL cast function

    XMLWordPrintable

    Details

      Description

      When working with JSON (or variant in general), it can sometimes happen that the type in JSON is smaller than the type I want to get via casting. Currently this will throw an exception. For example:

      //#CTL2
      const string JSON = 
      	'[{\
      		"id": 12345678890,\
      		"name": "abcd"\
      	  },\
      	  {\
      		"id": 12345,\
      		"name": "abcd"\
      	  }\
      	]';
      const variant PARSED = parseJson(JSON);
      
      function integer transform() {
      	for (integer i = 0; i < length(PARSED); ++i) {
      		printLog(info, "Next id: " + getID(PARSED, i));
      	}
      
      	return ALL;
      }
      
      function long getID(variant json, integer index) {
      	return cast(json[index]["id"], long);
      }
      

      Something like the above is pretty common (although simplified a lot) - grab an ID from each object in a list. The first one will work fine, but the second one will fail with java.lang.ClassCastException -> Cannot cast java.lang.Integer to java.lang.Long exception because the second ID will be parsed as integer.

      I know that I can catch the exception and inline the cast, but that still makes it quite annoying to deal with it:

      function long getID2(variant json, integer index) {
      	return cast(json[index]["id"], long) : cast(json[index]["id"], integer);
      }
      

      Please extend casting so that it can do safe upcasting without throwing an exception. I would imagine that the following casts would work fine:

      • integer to long
      • integer to decimal
      • long to decimal
      • number to decimal

      "Questionable" casts (i.e. those where I'm not sure whether I'd want that or not):

      • Any type into string (basically would call "toString" on the value). This can be nice to have, but on the other annoying if dealing with locale (since it would necessarily use default locale and that can be different than what is expected in given situation).
      • boolean to integer (or long): true = 1, false = 0
      • boolean to number
      • boolean to decimal

      Other casts (e.g. integer to string. string to integer etc.) would still throw an exception and would need to be handled by the developer.

      Note that it is possible to extend this even further to provide casting that tries the cast and if it works it would also allow downcasts. For example a long that fits into an integer can be safely cast while large numbers would throw an exception.
      I personally do not like these kinds of things since if not done correctly it can end up being like javascript with its crazy quirks, but on the other hand it would make it easier for users.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              slamam Martin Slama
              Reporter:
              repcekb Branislav Repcek
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 2 hours
                  2h