OCAML record type

I am creating an OCAML record type.

type matrix =
{m_index:int;n_index:int;matrix:float array array}
;;

I have a function that takes two arguments and an array.

let create_matrix m n ={m_index=m;n_index=n;matrix=Array.make_matrix~dimx:m_index~dimy:n_index 0.};;

However, I get the following error, can anyone explain why?

Error: Unbound value m_index

Upvotes: 0

Views: 232

Answers (1)

Jack
Jack

Reputation: 133577

The problem is that you are using m_index as a right hand value to initialize a field of a record that you are creating.

While initializing a record that contains field m_index you can't use this field in an expression assigned to another field of the same object.

Just use directly the m argument bound as the argument to create_matrix:

let create_matrix m n = {
  m_index = m;
  n_index = n;
  matrix = Array.make_matrix m n 0.
};;

Upvotes: 1

Related Questions