public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* Strange ClassCastException
@ 2014-09-21  5:16 Alcides Flores Pineda
  2014-09-21  7:54 ` Per Bothner
  2014-09-22  6:54 ` Per Bothner
  0 siblings, 2 replies; 4+ messages in thread
From: Alcides Flores Pineda @ 2014-09-21  5:16 UTC (permalink / raw)
  To: Kawa Mailing List

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Strange ClassCastException
  2014-09-21  5:16 Strange ClassCastException Alcides Flores Pineda
@ 2014-09-21  7:54 ` Per Bothner
  2014-09-22  6:54 ` Per Bothner
  1 sibling, 0 replies; 4+ messages in thread
From: Per Bothner @ 2014-09-21  7:54 UTC (permalink / raw)
  To: Alcides Flores Pineda, Kawa Mailing List



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
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Strange ClassCastException
  2014-09-21  5:16 Strange ClassCastException Alcides Flores Pineda
  2014-09-21  7:54 ` Per Bothner
@ 2014-09-22  6:54 ` Per Bothner
  2014-09-22 16:22   ` Alcides Flores Pineda
  1 sibling, 1 reply; 4+ messages in thread
From: Per Bothner @ 2014-09-22  6:54 UTC (permalink / raw)
  To: Alcides Flores Pineda, Kawa Mailing List



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
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Strange ClassCastException
  2014-09-22  6:54 ` Per Bothner
@ 2014-09-22 16:22   ` Alcides Flores Pineda
  0 siblings, 0 replies; 4+ messages in thread
From: Alcides Flores Pineda @ 2014-09-22 16:22 UTC (permalink / raw)
  To: Per Bothner; +Cc: Kawa Mailing List

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-09-22 16:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-21  5:16 Strange ClassCastException Alcides Flores Pineda
2014-09-21  7:54 ` Per Bothner
2014-09-22  6:54 ` Per Bothner
2014-09-22 16:22   ` Alcides Flores Pineda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).