Choice type implementation in F#

Viewed 76

I am fairly new in F# and having some problems with choice type implementation. I have three types.

type A = p of string
type B = {q:string;r:int}
type C = s of string

Then I've a choice type like this.

type D = A | B | C

Now I am having problems in creating D type of variable. I am trying to do something like this,

let v = {q="abc";r=12}
let vofD = D.B v

But it gives this error,

This value is not a function and cannot be applied

I have tried to search for documentation on this thing and it seems that I'm unable to find any good document or solution on this. Any help will be greatly appreciated.

1 Answers

The problem is that the F# compiler doesn't understand that D.B refers to type B. If you define your discriminated union like this:

type D =
   | FromA of A
   | FromB of B
   | FromC of C

Then you can write let vofD = D.FromB v.

Related