public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* building int[] arrays at runtime
@ 2017-02-01 23:12 Peter Lane
  2017-02-01 23:41 ` Per Bothner
  2017-02-01 23:45 ` Per Bothner
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Lane @ 2017-02-01 23:12 UTC (permalink / raw)
  To: kawa

Hi list,

I've been using Kawa as an R7RS implementation for a little while, and 
now am using Kawa to work with a Java library.  I've hit a little 
irritant when calling overloaded methods in Java.

If I build an int[] at runtime, I get a warning about more than one 
applicable method.  The code seems to run and give the right result, so 
the runtime does the right thing.  As the warning always appears first, 
when the script is loaded, I assume the script is compiled on loading 
and so the problem is discovered.

I understand why I'm getting this warning, but wondered if:

a. I should do something other than (apply int[] (list 3)) to create an 
int[] at runtime
b. Can I put a compile-time type hint in, to appease/suppress the 
initial checks?

I can turn the warnings off with: --warn-invoke-unknown-method=no
but that hides other potential problems, like a mis-spelt method name, 
until they trip a runtime exception.

Below is a simple example of what I'm doing:

Java class
----
public class Class1 {

   public void show (int[] x) {
     System.out.println ("show " + x[0]);
   }

   public void show (String s) {
     System.out.println ("show " + s);
   }
}
----

Kawa script
----
(define *x* (Class1:new))
(*x*:show "me")
(*x*:show (int[] 3))
----
and all is fine, I get the expected output:

$ kawa test.scm
show me
show 3

The problem arises when I try building the array at runtime:

----
(define *x* (Class1:new))
(*x*:show "me")
(*x*:show (apply int[] (list 3))); <----- built at runtime
----

$ kawa test.scm
test.scm:3:1: warning - more than one possibly applicable method 'show' 
in Class1
   candidate: void Class1.show(String)
   candidate: void Class1.show(int[])
show me
show 3


   thanks,

        Peter.

--
Peter Lane
http://peterlane.info/scheme.html

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

* Re: building int[] arrays at runtime
  2017-02-01 23:12 building int[] arrays at runtime Peter Lane
@ 2017-02-01 23:41 ` Per Bothner
  2017-02-02 18:43   ` Peter Lane
  2017-02-01 23:45 ` Per Bothner
  1 sibling, 1 reply; 4+ messages in thread
From: Per Bothner @ 2017-02-01 23:41 UTC (permalink / raw)
  To: Peter Lane, kawa

On 02/01/2017 03:12 PM, Peter Lane wrote:
> If I build an int[] at runtime, I get a warning about more than one applicable method.  The code seems to run and give the right result, so the runtime does the right thing.  As the warning always appears first, when the script is loaded, I assume the script is compiled on loading and so the problem is discovered.
>
> I understand why I'm getting this warning, but wondered if:
>
> a. I should do something other than (apply int[] (list 3)) to create an int[] at runtime

Yes, use the '@' splice operator:

(*x*:show (int[] @(list 3)))

This doesn't seem to be documented in the release manual, but it is documented
in the documentation for the "invoke" development branch:

http://per.bothner.com/kawa/invoke/#Application-and-Arguments-Lists
http://per.bothner.com/kawa/invoke/#Primitive-expression-syntax.idm140044326562704

(But note a bunch of this stuff doesn't work in the release or the master branch.
However, the splice operator '@' does work.)

> b. Can I put a compile-time type hint in, to appease/suppress the initial checks?

Use the 'as' special function:

(*x*:show (as int[] (apply int[] (list 3))))

That's basically a type cast.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

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

* Re: building int[] arrays at runtime
  2017-02-01 23:12 building int[] arrays at runtime Peter Lane
  2017-02-01 23:41 ` Per Bothner
@ 2017-02-01 23:45 ` Per Bothner
  1 sibling, 0 replies; 4+ messages in thread
From: Per Bothner @ 2017-02-01 23:45 UTC (permalink / raw)
  To: Peter Lane, kawa

On 02/01/2017 03:12 PM, Peter Lane wrote:
> I can turn the warnings off with: --warn-invoke-unknown-method=no
> but that hides other potential problems, like a mis-spelt method name, until they trip a runtime exception.

It is possible to control warnings locally:

   (with-compile-options warn-invoke-unknown-method: #f
     (*x*:show (apply int[] (list 3))))

But using the splice operator is definitely my recommendation:

    (*x*:show (int[] @(list 3 4 5))
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

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

* Re: building int[] arrays at runtime
  2017-02-01 23:41 ` Per Bothner
@ 2017-02-02 18:43   ` Peter Lane
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Lane @ 2017-02-02 18:43 UTC (permalink / raw)
  To: kawa

Thanks Per.


On 01/02/17 23:41, Per Bothner wrote:
> Yes, use the '@' splice operator:
>
> (*x*:show (int[] @(list 3)))

That does the trick!  And very naturally too.

> Use the 'as' special function:
>
> (*x*:show (as int[] (apply int[] (list 3))))
>
> That's basically a type cast.

Thanks for the tip.  That has helped clear up some other warnings I've 
been getting.

--
Peter Lane
http://peterlane.info/scheme.html

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

end of thread, other threads:[~2017-02-02 18:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-01 23:12 building int[] arrays at runtime Peter Lane
2017-02-01 23:41 ` Per Bothner
2017-02-02 18:43   ` Peter Lane
2017-02-01 23:45 ` Per Bothner

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).