Enumerations in F # for Strings

I am working on an outdated database without any systems that I can change. Due to type providers, I decided to use F #. Now it’s not so simple that I delve into it. How to create a field in my database that has only the following lines? This is similar to ENUM. I tried the following but did not get it.

Attempt 1

type SuspendedDriver = "SI"
type ActiveDriver = "IMPRESO"
type Applicant = "NO"
type TemporaryDriver = "TEMP"
type Uncertain = "NULL"

type DriverStatus =
    | SuspendedDriver
    | ActiveDriver
    | Applicant
    | TemporaryDriver
    | Uncertain

type Driver = {
    status : DriverStatus
    }

Error

Error FS0618: Invalid literal in type (FS0618) (ScriptTest)

Attempt 2

type DriverStatus =
    | SuspendedDriver = "SI"
    | ActiveDriver = "IMPRESO"
    | Applicant = "NO"
    | TemporaryDriver = "TEMP"
    | Uncertain = "NULL"

Error

Error FS0951: Literal enumerations must have type int, uint, int16,
uint16, int64, uint64, byte, sbyte or char (FS0951) (ScriptTest)
+4
source share
1 answer

As mentioned in the comment, you cannot define an enumeration that has values ​​that are erased for strings. Enums can only be integers (of different types).

Using Enums in an Ugly Way

, Enum.Parse, , , , . :

type DriverStatus =
   | SI = 0

System.Enum.Parse(typeof<DriverStatus>, "SI") :?> DriverStatus

, . , , ( - ):

type DriverStatus =
   | SuspendedDriver
   | ActiveDriver

let parseDriverStatus = function
   | "SI" -> SuspendedDriver
   | "IMPRESO" -> ActiveDriver 
   | code -> failwith (sprintf "Wrong driver code! %s" code)

Cool Enum SQL provider

, MS SQL, SQL Command Provider , SQL , , .

+5

Source: https://habr.com/ru/post/1652059/


All Articles