Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4476

Typecasting problem on list items

    XMLWordPrintableJSON

Details

    • Patch, Important

    Description

      I was trying to add the following into a thrift interface file.

      const list<double> timeCoefficients = [24, 60, 60, 1000, 1000, 1000]

      With the definition given above the .thrift file compiled properly. However, I noticed that in Python, the items in timeCoefficients are considered to be integer although the list has been defined to include items of double type. Then I modified the definition as given below to make sure all the items are of type double

      const list<double> timeCoefficients = [24.0, 60.0, 60.0, 1000.0, 1000.0, 1000.0]

      After the change, I ran into the following error during compilation.

      [ERROR] .../TimeCoefficients.java:[402,48] error: no suitable method found for add(int)
      [ERROR] method Collection.add(Double) is not applicable
      [ERROR] (argument mismatch; int cannot be converted to Double)
      [ERROR] method List.add(Double) is not applicable
      [ERROR] (argument mismatch; int cannot be converted to Double)

      Next, I changed the line as follows and the compilation became successful.

      const list<double> timeCoefficients = [24.1, 60.1, 60.1, 1000.1, 1000.1, 1000.1]

      When I reviewed the generated Java source files, I discovered that

      const list<double> timeCoefficients = [24, 60, 60, 1000, 1000, 1000]

      compiles to

      public static final java.util.List<java.lang.Double> timeCoefficients = new java.util.ArrayList<java.lang.Double>();
      static {

        timeCoefficients.add((double)24);
        timeCoefficients.add((double)60);
        timeCoefficients.add((double)60);
        timeCoefficients.add((double)1000);
        timeCoefficients.add((double)1000);
        timeCoefficients.add((double)1000);

      }

      whilst

      const list<double> timeCoefficients = [24.0, 60.0, 60.0, 1000.0, 1000.0, 1000.0]

      compiles to

      public static final java.util.List<java.lang.Double> timeCoefficients = new java.util.ArrayList<java.lang.Double>();
      static {
        timeCoefficients.add(24);
        timeCoefficients.add(60);
        timeCoefficients.add(60);
        timeCoefficients.add(1000);
        timeCoefficients.add(1000);
        timeCoefficients.add(1000);
      }

      which leads to an error.

      When I modified this line as follows

      const list<double> timeCoefficients = [24.1, 60.1, 60.1, 1000.1, 1000.1, 1000.1]

      this line compiled to

      public static final java.util.List<java.lang.Double> timeCoefficients = new java.util.ArrayList<java.lang.Double>();
      static {
        timeCoefficients.add(24.1);
        timeCoefficients.add(60.1);
        timeCoefficients.add(60.1);
        timeCoefficients.add(1000.1);
        timeCoefficients.add(1000.1);
        timeCoefficients.add(1000.1);
      }

      My guess is that, even if I put .0 at the end of each numeric constant, on the Java side, Thrift compiler considers them to be double and does no typecasts. However, the .0 s are getting lost somewhere and the items become integers in the generated Java code. Thus, when it comes to populating the array, Java cannot succeed. Double s cannot be unboxed to integer and Java thinks int and Double are not related.

      Attachments

        Activity

          People

            jking3 James E. King III
            ozymaxx Ozan Can Altiok
            Votes:
            3 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: