Reputation: 528
Algebraic data types are represented in F# by using Sum (Discriminated Unions) and product (tuples) types. I can then combine these elements as in algebra.
I can also define a tuple of two class types like this :
type MINT (i:int) =
member x.i = i
type MINTtuple = MINT * MINT
let v :MINTtuple= MINT(42), MINT(43)
(fst v).i |> printfn "%d" |> ignore
But I cannot seem to define a DU containing class types like this or any other syntax I could think of after looking at some documentation :
type MINTDU =
|Positive of MINT
|Negative of MINT
let w = Positive MINT(45)
My question : if it is not possible, what are the fundamental reasons ? I am very happy with F# features as they are. I only want to better understand what are the tensions between functional types and class types. Thanks.
Upvotes: 0
Views: 189
Reputation: 2291
You just need some brackets. Try this:
let createPositiveMint i =
Positive (MINT i)
If you don't want a separate function as in your sample it would just be
let w = Positive (MINT 45)
Edited as per @kaefer suggestion.
Upvotes: 1