Strange ClassCastException

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Strange ClassCastException

Alcides Flores Pineda
Hello Everybody:

I've just stumbled on a very (IMHO) strange exception case while running a
trivial programming example in the Kawa REPL (and actually interpreting
it as a file too)

The example is the classical basic/begginer's program to calculate the
area of a circle.

Here is the code just as I typed it in the REPL (and saved it in a file too):

>  #|kawa:1|# (define (circle-area radius) (* java.lang.Math:PI (expt radius 2)))

And I got this result/exception when calling it (or running the file
with kawa -f):

>#|kawa:2|# (circle-area 3)
>java.lang.ClassCastException: java.lang.Double cannot be cast to gnu.math.Numeric
>    at atInteractiveLevel$5.circleArea(stdin:1)
>    at atInteractiveLevel$5.apply1(stdin:1)
>    at gnu.expr.ModuleMethod.apply1(ModuleMethod.java:192)
>    at gnu.expr.ModuleMethod.apply(ModuleMethod.java:163)
>    at gnu.mapping.CallContext.runUntilDone(CallContext.java:234)
>    at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:380)
>    at gnu.expr.ModuleExp.evalModule(ModuleExp.java:217)
>    at kawa.Shell.run(Shell.java:290)
>    at kawa.Shell.run(Shell.java:203)
>    at kawa.Shell.run(Shell.java:184)
>    at kawa.repl.main(repl.java:892)

Now, IMHO the strange thing is that if I eval the same forms outside the
function definition, it all works well and I get the expected result:

>#|kawa:3|# (define radio 3)
>#|kawa:4|# (* java.lang.Math:PI (expt radio 2))
>28.274333882308138

I know that for this basic/trivial example I can workaround it with this:

> (define (circle-area radius)(* java.lang.Math:PI (* radius radius)))

So, my question is: Is this a known bug/feature? Should I submit a bug report?
Is it not-recommended to mix Java and Scheme library functions?

Thanks in advance.

Greetings.
--
Alcides Flores Pineda.
Reply | Threaded
Open this post in threaded view
|

Re: Strange ClassCastException

Per Bothner


On 09/20/2014 10:11 PM, Alcides Flores Pineda wrote:
> So, my question is: Is this a known bug/feature?

It's a bug.  Originally, Kawa arithmetic only worked for "Kawa numbers"
i.e. the classes is gnu.math.  But at this point you're supposed to
be able to mix and match "Kawa numbers" and "Java numbers".

The error "java.lang.Double cannot be cast to gnu.math.Numeric" is because
we're using a raw cast to gnu.math.Numeric (i.e. a Kawa numbers), though
the actual return type in this case is a "Java number".  We need to
either use a "smart cast" (i.e. gnu.kawa.listexpr.LangObjType.coerceNumeric)
or fix the return type.

The former is a only line fix, but I should also consider tweaking the
calculated return type.

> Should I submit a bug report?

Not needed.  (But if I don't report a fix in a day or two, remind me.)

> Is it not-recommended to mix Java and Scheme library functions?

It's supposed to work.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: Strange ClassCastException

Per Bothner
In reply to this post by Alcides Flores Pineda


On 09/20/2014 10:11 PM, Alcides Flores Pineda wrote:
> Here is the code just as I typed it in the REPL (and saved it in a file too):
>
>>   #|kawa:1|# (define (circle-area radius) (* java.lang.Math:PI (expt radius 2)))
>
> And I got this result/exception when calling it (or running the file
> with kawa -f):
>
>> #|kawa:2|# (circle-area 3)
>> java.lang.ClassCastException: java.lang.Double cannot be cast to gnu.math.Numeric

I checked in a fix for this.  I also checked in a testcase based on your report.

I'm also thinking about some tweaks to code-generation that avoid this problem,
but the current check-in should be ok for now.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: Strange ClassCastException

Alcides Flores Pineda
Hi Per:

Thank you very much for your help and your quick response.
My little program is working well now. :-)

Greetings.
--
Alcides Flores Pineda

On Mon, Sep 22 2014, Per Bothner wrote:

> On 09/20/2014 10:11 PM, Alcides Flores Pineda wrote:
>> Here is the code just as I typed it in the REPL (and saved it in a file too):
>>
>>>   #|kawa:1|# (define (circle-area radius) (* java.lang.Math:PI (expt radius 2)))
>>
>> And I got this result/exception when calling it (or running the file
>> with kawa -f):
>>
>>> #|kawa:2|# (circle-area 3)
>>> java.lang.ClassCastException: java.lang.Double cannot be cast to gnu.math.Numeric
>
> I checked in a fix for this.  I also checked in a testcase based on your report.
>
> I'm also thinking about some tweaks to code-generation that avoid this problem,
> but the current check-in should be ok for now.