Tom Squires
Tom Squires

Reputation: 9296

F# 2d array index error

The below code gives the error The operator 'expr.[idx]' has been used an object of indeterminate type based on information prior to this program point. Consider adding further type constraints. I think i have told it the type. What is wrong?

let board = Array2D.init 30 30 (fun x y -> 0)

let tickCell x y board : int[,]  =
    match board  with
    |board when board.[x].[y] = 0-> 1
    |board when board.[x].[y] = 1-> 0
    | _ -> -1

let board2 = Array2D.init 30 30 (fun x y -> tickCell x y board)

Upvotes: 1

Views: 806

Answers (1)

curtisthibault
curtisthibault

Reputation: 209

These small changes should get rid of the error:

  1. Wrap the board parameter and type in parenthesis
  2. Access the cell with [x,y]

    let tickCell x y (board : int[,])  =
        match board  with
        |board when board.[x,y] = 0-> 1
        |board when board.[x,y] = 1-> 0
        | _ -> -1
    

Upvotes: 2

Related Questions