How do I specify a type that shares a type definition with another type?
The following code does not compile:
[<Test>]
let ``move checker``() =
{ Position={ X=1; Y=1 } } |> moveBlack NorthEast
|> should equal { Position={ X=2; Y=2 } }
This is because the record that I'm passing into the moveBlack function is mapped to RedChecker instead of BlackChecker.
Type mismatch. Expecting a RedChecker -> 'a but given a BlackChecker -> BlackChecker The type 'RedChecker' does not match the type 'BlackChecker'
More than likely, this error occurs because the last type to have this definition is RedChecker:
type BlackChecker = { Position:Position }
type RedChecker = { Position:Position }
I thought I could specify the black checker by doing this:
(BlackChecker:{ Position={ X=1; Y=1 } })
And thus have:
[<Test>]
let ``move checker``() =
(BlackChecker:{ { Position={ X=1; Y=1 } }) |> moveBlack NorthEast
|> should equal (BlackChecker:{ { Position={ X=2; Y=2 } })
However, the above code also doesn't compile.
Here's the rest of my code:
(* Types *)
type Color = | Red | Black
type North = NorthEast | NorthWest
type South = SouthEast | SouthWest
type Position = { X:int; Y:int }
type BlackChecker = { Position:Position }
type RedChecker = { Position:Position }
(* Functions *)
let moveBlack (direction:North) (checker:BlackChecker) =
match direction with
| NorthEast -> { checker with Position= { X=2; Y=2 } }
| NorthWest -> { checker with Position= { X=1; Y=2 } }
(* Tests *)
[<Test>]
let ``move checker``() =
{ Position={ X=1; Y=1 } } |> moveBlack NorthEast
|> should equal { Position={ X=2; Y=2 } }