I'm about to write a 2d game using LibGDX which has nice classes like Vector2
.
Since I'm writing in functional fashion (copying, pattern matching, etc.) I'd like it to be a case class.
Because it's not one, I decided to try to extend it without success:
import com.badlogic.gdx
class Vector2(x: Float, y: Float) extends gdx.math.Vector2(x, y) {
def copy(x: Float = this.x, y: Float = this.y): Vector2 = {
new Vector2(x, y)
}
}
object Vector2 {
def apply(x: Float = 0, y: Float = 0): Vector2 = {
new Vector2(x, y)
}
}
The problem is that the following throws println(Vector2(1, 2).x)
because there's no x
field.
If I write it like class Vector2(override val x: Float, override val y: Float)
it throws:
overriding variable x in class Vector2 of type Float; value x has incompatible type class Vector2(override val x: Float, override val y: Float) extends gdx.math.Vector2(x, y) {
I'm unable to get this to work. I also tried var
instead and java.lang.Float
without success.
Edit: It seems that it's not possible, because Scala generators a function and there's no equivalent function in Java code.