If the expression causes an error like elm

I want to update an entry in type Elm

type CubeData = {currDirection : Vec3, translation : Vec3, transform : Mat4}

Why can I use the following code.

updateCubeData : CubeData -> CubeData
updateCubeData cubeData = {cubeData | translation <- cubeData.currDirection,
                                      transform <- translate cubeData.translation cubeData.transform}

However, now I want to update the record field differently depending on the current value of the field, so I tried to do this

 updateCubeData : CubeData -> CubeData
 updateCubeData cubeData = if abs cubeData.translation.x > 2.0 || abs cubeData.translation.y > 2.0
                           then  {cubeData | translation <- cubeData.currDirection,
                                             transform <- translate cubeData.translation cubeData.transform}
                           else  {cubeData | currDirection <- negate cubeData.currDirection,
                                             translation <- cubeData.currDirection,
                                             transform <- translate cubeData.translation cubeData.transform}

This is a type error that I reproduced below. Note that the type signature updateCubeDatais the 12th line of the file.

Type error on line 15, column 68 to 76:
        cubeData

   Expected Type: {a | y : Float, x : Float}
     Actual Type: Vec3

Type error on line 18, column 68 to 76:
        cubeData

   Expected Type: {a | y : Float, x : Float}
     Actual Type: Vec3

Type error between lines 13 and 18:
        if | ((abs cubeData.translation.x) > 2.0) ||
             ((abs cubeData.translation.y) > 2.0) ->
               {cubeData |
                    translation <- cubeData.currDirection,
                    transform <- translate cubeData.translation cubeData.transform}
           | True ->
               {cubeData |
                    currDirection <- negate cubeData.currDirection,
                    translation <- cubeData.currDirection,
                    transform <- translate cubeData.translation cubeData.transform}

   Expected Type: Vec3
     Actual Type: {a | y : Float, x : Float}

If I exclude the operator ifand just set the function equal to the contents of thenand else, the code compiles and executes as expected (if this block was the whole method). Why is adding an if statement like I am here introducing this type error and how to fix it?

+4
source share
1 answer

:

abs cubeData.translation.x > 2.0 || abs cubeData.translation.y > 2.0

, cubeData.translation Vec3, , {a | y : Float, x : Float}. Vec3 , x y.

, getX getY Math.vector3:

abs (cubeData.translation |> getX) > 2.0 || abs (cubeData.translation |> getY) > 2.0

+6

Source: https://habr.com/ru/post/1545439/


All Articles