3
votes

I have defined both records and created a union from them, but F# still complains that the constructor "Food" is not defined. What is the problem?

type Product = {Name : string; BasePrice: int}
type Size = {Medium: int; Large: int}

type Food = | Product of Product| Size of Size

let food = Food({Name = "Bagel"; BasePrice = 20}; {Medium = 10; Large = 20})
1
Food is a type not a constructor, you need something like let food = Product {Name = "Bagel"; BasePrice = 20}. Are you trying to build a list Food? - Lee
@Lee I think you have the correct answer. Pls, add it as an answer to this question. This way Denki can mark it as the correct answer and this will add to your reputation. - Norbert B.

1 Answers

4
votes

Food is a type, not a constructor - to create a value of type Food you need to use one of the constructors Product or Size. It looks like you're trying to construct a list Food, in which case you can use:

let food = [Product {Name = "Bagel"; BasePrice = 20}; Size {Medium = 10; Large = 20}]