This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: numeric literals


On 10/25/2011 03:45 PM, Jamison Hope wrote:
Is there a simple syntax which will get Kawa to read integral
literals as the appropriate Java SE number types, rather than
the gnu.math types?

I think the place to fix this is in the visitQuoteExp method in InlineCalls. This method handles the case of an integer literal, and if the required type is a primitive integer type *and* the literal fits in the primitive type, then it quietly converts it.

It seems reasonable to extent thid for the case that the required
type is Integer, for example.

Perhaps the cleanest might be something like:

  // Maybe in PrimType - compare PrimType#boxedType.
  PrimType unboxedType(Type type) {
     if (type instanceof PrimType)
         return (PrimType) type;
     if (! (type instanceof ClassType))
         return null;
     if ("java.lang.Integer".equals(type.getName()))
         return PrimType intType;
     ...
     return null;
  }

Then in visitQuoteExp:

    PrimType primRequired = PrimType.unboxedType(required);
    if (primRequired instanceof PrimType && ! exp.isExplicitlyTyped())
          {
            char sig1 = primRequired.getSignature().charAt(0);
     etc

We should also allow silent conversion from IntNum to BigInteger.
That might be reasonable for more than just literals, but at
least literals IntNum to BigInteger is straight-forward and obvious.

You want to whip up a patch?

A general silent conversion from IntNum to BigInteger or vice versa does make
sense semantically, but it's expensive at run-time. But we should allow
explicit conversions, and there seems to be some problems:


#|kawa:2|# (as java.math.BigInteger 122)
/dev/stdin:2:26: warning - type integer is incompatible with required type java.math.BigInteger
/dev/stdin:2:1: warning - cannot convert literal (of type gnu.math.IntNum) to java.math.BigInteger
Invalid parameter, was: gnu.math.IntNum cannot be cast to java.math.BigInteger
java.lang.ClassCastException: gnu.math.IntNum cannot be cast to java.math.BigInteger
at atInteractiveLevel$2.run(stdin:2)
at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:279)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:180)
at kawa.Shell.run(Shell.java:280)
at kawa.Shell.run(Shell.java:194)
at kawa.Shell.run(Shell.java:175)
at kawa.repl.main(repl.java:884)
#|kawa:7|# (as gnu.math.IntNum (java.math.BigInteger "1234"))
/dev/stdin:7:21: warning - type java.math.BigInteger is incompatible with required type integer
Invalid parameter, was: java.math.BigInteger cannot be cast to gnu.math.IntNum
java.lang.ClassCastException: java.math.BigInteger cannot be cast to gnu.math.IntNum
at atInteractiveLevel$7.run(stdin:7)
at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:279)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:180)
at kawa.Shell.run(Shell.java:280)
at kawa.Shell.run(Shell.java:194)
at kawa.Shell.run(Shell.java:175)
at kawa.repl.main(repl.java:884)
--
--Per Bothner
per@bothner.com http://per.bothner.com/



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]