2

i have a Java-Interface that i want to implement in Scala. The Interface contains the following method-declaration:

  public Boolean existsTable(String tableId);

My Scala-Implementation is:

  override def existsTable(tableId: String): Boolean = {
    val dbmeta = connection.getMetaData()
    val dbtables = dbmeta.getTables(null, null, channelId, null)
    if (dbtables.next())
      // table exists
      return true
    else
      return false
  }

For some reason i get the error "overriding method existsTable in trait xyz of type (tableId: String)Boolean; method existsTable has incompatible type"

But i dont really know where there could be an incompatible type since its just a Boolean that is the return-type?

Greetings.

PS: I had this method working once before in the exact way, when i wasnt implementing an interface, so there should be no error in the method itself. It's just the Implementation of the Java-Interface in Scala that's giving me a hard time.

3
  • note: public modifier on interface method declarations is redundant; also, why not just return dbtables.next()? (and why Boolean and not boolean while we're at it?) Commented Jun 13, 2013 at 14:34
  • Hello, well if i write "boolean" then scala doesn't recognize that type. :) But i changed it to just return dbtablex.next(), so thx for that. Unfortunately it's still complaining about the incompatible type... Commented Jun 13, 2013 at 14:38
  • 3
    you need java's Boolean: override def existsTable(tableId: String): java.lang.Boolean. Commented Jun 13, 2013 at 14:43

2 Answers 2

3

The reason why this is not working, as indicated in the comments is that Scala's Boolean is not the same as Java's Boolean. A quick way to fix this is:

import java.lang.{Boolean => JBoolean}

override def existsTable(tableId: String): JBoolean

By aliasing the type, it's a little more clear what you meant to return. You could of course just define it as:

override def existsTable(tableId: String): java.lang.Boolean
Sign up to request clarification or add additional context in comments.

1 Comment

Both this and the answer suggested in the comments force the use of the Java Boolean type, which is less interoperable with Scala, and forces boxing all the time (which looks unnecessary in this situation). Changing the Java interface to use boolean would be better.
2

The problem is that Scala's Boolean corresponds to Java's boolean, not Java's Boolean. If you change the Java return type to boolean but leave the Scala one as Boolean it should work.

Scala raises all primitive types to the level of real objects, letting the compiler decide to box/unbox them as necessary depending on how they're used. Java, on the other hand, maintains two representations of primitive types: the primitive type itself (small letter) and a boxed representation (capital letter).

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.