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: Multiple possible matches for constructor


Mark Wutka wrote:
I was trying to write a little function to open a socket and return an
input port and an output port as values. It seems to work, but
originally I was getting warnings about multiple possibly applicable
methods, since Socket can take InetAddress or String as the first
parameter. The only way I could find to eliminate the warning was to use
(primitive-constructor <java.net.Socket> (<java.lang.String> <int>))

Use a cast or a type specifier. I suggest:


(define (socket-connect (host :: <String>) (port :: <int>))
   ...)

What I am wondering is, does it really matter? The original code that
just called java.net.Socket:new seems to work just fine, I just don't
know if one is any more efficient than the other.

It makes a big difference. If you get the warning, it means Kawa cannot pick the right methods at compile-time, but instead has to use run-time reflection, which is a *lot* slower.

Also, many of these warnings may be due to a bug in your program, so it
is definitely worthwhile trying to fix them.  Also, you might consider
compiling with --warn-undefined-variable which catches even more errors.

I guess if I leave it the old way, my function can take either String or
InetAddress as a parameter.

That is true, but if you want that it's more efficient (if you care) to do the test yourself:

(if (instance? host <java.net.InetAddress>))
  (java.net.Socket:new (as <java.net.InetAddress> host) port)
  (java.net.Socket:new (as <String> host) port))
--
	--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]