0

I want to create companion objects for some imported Java types, so that I do not have to use new to allocate them. I want to start with the type Vector3f is imported from com.jme3.math from jMonkeyEngine.

What I tried is:

package com.jme3.math

object Vector3f {
  def apply() = new Vector3f()
  def apply(x:Float, y:Float, z:Float) = new Vector3f(x,y,z)
}

When compiling this, I get errors:

Error:(8, 21) not found: type Vector3f def apply() = new Vector3f()

When I add import com.jme3.math.Vector3f, I get warning which probably explains what I see:

Warning:(3, 22) imported `Vector3f' is permanently hidden by definition of object Vector3f in package math

How can I create a companion object for com.jme3.math.Vector3f or other types imported from Java?

Suma
  • 33,181
  • 16
  • 123
  • 191
  • possible duplicate of [In Scala, how can I define a companion object for a class defined in Java?](http://stackoverflow.com/questions/4921827/in-scala-how-can-i-define-a-companion-object-for-a-class-defined-in-java) – Suma Sep 04 '14 at 11:59

1 Answers1

1

That's a naming issue, you can't have both the Java class and Scala companion object with the same name there.

Either you access Java class inside companion object with the fully qualified name new com.jme3.math.Vector3f(...) or you indicate a different local name while importing it.

import com.jme3.math.{ Vector3f => JV3 }

def apply(): JV3 = new JV3()

Extra example (companion object for Java class org.apache.http.HttpClient):

import org.apache.http.client.{ HttpClient ⇒ HC }

object HttpClient {
  def apply(): HC = ???
}

Or...

// No import
object HttpClient {
  def apply(): org.apache.http.client.HttpClient = ???
}
cchantep
  • 9,118
  • 3
  • 30
  • 41
  • I have tried "access Java class inside companion object with the fully qualified name new com.jme3.math.Vector3f", however this still gives me error: "type Vector3f is not a member of package com.jme3.math". I guess I perhaps should not use the same package name as the Java I am importing? ^ – Suma Sep 04 '14 at 10:00
  • 1
    You should not define your companion object with the same package name. First that creates technical issue, then only class of the original Java lib must be defined in this package to respect authorship and responsability distinction. – cchantep Sep 04 '14 at 10:18
  • When I use a different package name, it kind of works. There is no error, but there is warning "imported `Vector3f' is permanently hidden by definition of object Vector3f in package tree". However there are no errors and in spite of the warning Vector3f seems to work OK, even in the companion object itself there is no need to specify com.jme3.math, I can use just = new Vector3f. Strange. – Suma Sep 04 '14 at 11:38
  • I do not know how to proceed with the question now. It seems using the name of the package was so important mistake it rendered the rest of the question moot. However, if I edit the question to use a different package name, the question will change a lot (it will now be only about the warning) and your response will be mostly outdated. – Suma Sep 04 '14 at 11:42
  • This warning meaning you import a class whose name correspond to a class or object in your Scala code. You should neither define your code in the same package, nor use conflicting name (either by renaming in the import statement, or using FQDN class name *without* import). – cchantep Sep 04 '14 at 11:44
  • It reminds me of Catch 22 - companion object needs (by the definition) to have the same name as the class it is accompanying. Does this mean I cannot create a companion object for an imported class? – Suma Sep 04 '14 at 11:49
  • Would you mind taking care you what's written in the answer and comments please? As long as you respect the few rules, you can define the companion. – cchantep Sep 04 '14 at 12:31