This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
numeric literals
- From: Jamison Hope <jrh at theptrgroup dot com>
- To: Kawa mailing list <kawa at sourceware dot org>
- Date: Tue, 25 Oct 2011 18:45:41 -0400
- Subject: numeric literals
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?
For instance, "3.14" is read as a DFloNum, but "3.14d0" is
read as a Double and "3.14f0" a Float. So, I can do this:
#|kawa:1|# (define floats ::java.util.List[java.lang.Float]
(java.util.ArrayList[java.lang.Float]))
#|kawa:2|# (floats:add 5.0f0)
#t
where an unadorned literal would lead to hilarity:
#|kawa:3|# (floats:add 3.14)
/dev/stdin:3:13: warning - type DFloNum is incompatible with
required type java.lang.Float
/dev/stdin:3:1: warning - no possibly applicable method 'add' in
ClassType java.util.List<ClassType java.lang.Float>
#t
Note that despite the warning and the pronouncement that there is
"no possibly applicable method" at Kawa's disposal, the DFloNum
was successfully added to the List and add() returned true.
But that's presumably due to type erasure; it's still a DFloNum.
#|kawa:4|# floats
[5.0, 3.14]
#|kawa:5|# (*:get-class (floats 0))
class java.lang.Float
#|kawa:6|# (*:get-class (floats 1))
Argument #1 '3.14' to 'java.lang.Float.getClass()' has wrong type
(DFloNum) (gnu.math.DFloNum cannot be cast to java.lang.Float)
at atInteractiveLevel$6.run(stdin:6)
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)
Caused by: java.lang.ClassCastException: gnu.math.DFloNum cannot be
cast to java.lang.Float
... 7 more
The situation for Integers (and Shorts and Longs and Bytes)
is a little different.
#|kawa:7|# (define ints ::java.util.List[java.lang.Integer]
(java.util.ArrayList[java.lang.Integer]))
#|kawa:8|# (ints:add 0)
/dev/stdin:8:11: warning - type integer is incompatible with
required type java.lang.Integer
/dev/stdin:8:11: warning - type integer is incompatible with
required type java.lang.Integer
/dev/stdin:8:11: warning - cannot convert literal (of type
gnu.math.IntNum) to java.lang.Integer
Argument #2 '0' to 'java.util.List.add(java.lang.Integer)' has wrong
type (integer) (gnu.math.IntNum cannot be cast to java.lang.Integer)
at atInteractiveLevel$8.run(stdin:8)
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)
Caused by: java.lang.ClassCastException: gnu.math.IntNum cannot be
cast to java.lang.Integer
... 7 more
#|kawa:9|# ints
[]
In this case, I get the somewhat confusing "an integer is
not an Integer" warning, similar to the "DFloNum is
incompatible with required type java.lang.Float" above,
but then the add() completely fails and 0 isn't even added
to the List as an IntNum.
I can manually cast to an int (which then is automagically
boxed to an Integer), but that seems inelegant.
#|kawa:10|# (ints:add (as int 42))
#t
#|kawa:11|# ints
[42]
--
Jamison Hope
The PTR Group
www.theptrgroup.com