f# - How do I specify a type that shares a type definition with another type? -
how specify type shares type definition type?
the following code not compile:
[<test>] let ``move checker``() = { position={ x=1; y=1 } } |> moveblack northeast |> should equal { position={ x=2; y=2 } }
this because record i'm passing moveblack function mapped redchecker instead of blackchecker.
type mismatch. expecting redchecker -> 'a given blackchecker -> blackchecker type 'redchecker' not match type 'blackchecker'
more likely, error occurs because last type have definition redchecker:
type blackchecker = { position:position } type redchecker = { position:position }
i thought specify black checker doing this:
(blackchecker:{ position={ x=1; y=1 } })
and have:
[<test>] let ``move checker``() = (blackchecker:{ { position={ x=1; y=1 } }) |> moveblack northeast |> should equal (blackchecker:{ { position={ x=2; y=2 } })
however, above code doesn't compile.
here's rest of 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 | northeast -> { checker position= { x=2; y=2 } } | northwest -> { checker position= { x=1; y=2 } } (* tests *) [<test>] let ``move checker``() = { position={ x=1; y=1 } } |> moveblack northeast |> should equal { position={ x=2; y=2 } }
the labels of declared type take precedence on of declared type. should able this:
({ blackchecker.position={ x=1; y=1 } })
Comments
Post a Comment