Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

f# 3.0 - Choice type implementation in F#

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.

question from:https://stackoverflow.com/questions/65861245/choice-type-implementation-in-f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...