0

I am calling a method that returns an array:

val localTrustManagerFactory =
  TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
val localTrustManager =
  ((localTrustManagerFactory.getTrustManagers).apply(0)).asInstanceOf[X509TrustManager]

If I leave out the .apply call, I get a compile-time error:

val localTrustManager =
  ((localTrustManagerFactory.getTrustManagers)(0)).asInstanceOf[X509TrustManager]
error: too many arguments for method getTrustManagers: ()Array[javax.net.ssl.TrustManager]

Is there any better way to retrieve an array element? I though that the compiler would supply the apply method call implicitly.

UPDATE: The classes used in this code are from the standard Java library:

import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager

I don't know if the Scala compiler (2.9.2-1) interprets getTrustManagers as having an argument list or not.

1
  • I can't reproduce this. How is getTrustManagers defined and which scala version are you using? This really looks like a compiler bug. Commented Jun 15, 2012 at 20:01

1 Answer 1

4

Your method getTrustManagers seems to be defined with an empty argument list, that is

def getTrustManagers(): Array[TrustManager]

rather than with no argument list

def getTrustManagers: Array[TrustManager]

What you have is a corner case, scala tries to be flexible as to dropping the empty argument list at call site, but it could be ambiguous if the apply was dropped at the same time (e.g, if there was an apply method with no argument, or if getTrustManagers was overloaded and had a version with one argument).

You can do getTrustManagers()(0) (same as in java, except for the (0) instead of [0].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.