Getting "Unbound record field mid_x"

45 Views Asked by At

I am not understanding why I am getting this error: Unbound record field mid_x when running this code. What the function midpoint_segment is trying to do is find the midpoint of a given type segment and return a point type.

type point = 
  {x : float; y : float} 

type segment = 
  {startp : point; endp : point}

let midpoint_segment {startp; endp} =
  let mid_x = (startp.x +. endp.x) /. 2.0 in
  let mid_y = (startp.y +. endp.y) /. 2.0 in
  {mid_x; mid_y}

What does this error mean? I am defining mid_x and mix_y with let.

1

There are 1 best solutions below

3
glennsl On

{mid_x; mid_y} is syntax sugar for the record literal {mid_x = mid_x; mid_y = mid_y}.

If you want to create a record of type point you either need to specify the record field labels explicitly: {x = mid_x; y = mid_y} or, to use field punning, rename mid_x and mid_y to x and y in order to do {x; y}.