I am trying to create a new vector of class type Vec2(x:Double, y:Double) program is unable to find x and y and returns a mismatch error.

When trying to declare x and y separately I am met with an error "Expression of type (Double, Double) does not conform to expected type Vec2.

Vec2(x:Double, y:Double)
val sum:Vec2 = (x:Double , y:Double)

Wanting to use the sum vector in the program but am unable to declare it.

1 Answers

1
Tim On Best Solutions

There are a couple of errors here:

val sum:Vec2 = (x:Double , y:Double)

Firstly, you don't need to specify the types of the values here, so it could be

val sum: Vec2 = (x, y)

Secondly, the value (x, y) is a tuple and there is no automatic mechanism for creating classes from tuples. So you need to specify the class explicitly:

val sum:Vec2 = Vec2(x, y)

Finally, the type of sum is obvious from this statement, so it can be omitted:

val sum = Vec2(x, y)