Hi,
I'm not sure if using F# discriminated union type here is the right thing. The point of discriminated unions is that their constructors (e.g. One in your example) can have some parameters, so you can for example write:

1
2
3
4
 
type Vehicle = 
  | Car of int  // int specifies the number of doors...
  | MotorCycle

Then you'd have to provide some value when creating the "Car" case and how would you determine that argument? Would you want to create all possible cars? It seems to me that using some other type would be more appropriate. In fact, from your example, it looks like you could use just an integer, but there is probably some reason that you haven't mentioned.

Anyway, you could also use enums - enum is a bit like discriminated union, but constructors don't have any parameters, but instead have an associated numeric value:

1
2
3
4
 
type Simple = 
  | One = 1
  | Two = 2

Then you can get all values in an array by calling Simple.GetValues(typeof<Simple>).
To convert between an integer and Simple type, you can use the unbox primitive:

1
2
3
 
let simple = unbox<Simple> 2 
let num = unbox<int> Simple.One
By on 5/25/2009 7:38 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper