Closed Dzoukr closed 2 years ago
As I promised, I am updating this with description of how I create enum(-ish) types in TP. I am pretty sute this is not the cleanest way, but for my usecase where every "Enum" case needs to hold background string
value (instead of int
), this works ok.
let enumValues = ["optionA";"optionB";"optionC"]
let enumType = ProvidedTypeDefinition(assembly, namespace, "MyEnumType", None)
enumType.SetEnumUnderlyingType(typeof<string>)
enumValues |> List.iter (fun x ->
let n = x |> Names.pascalName
enumType.AddMember(ProvidedField.Literal(n, enumType, x))
)
enumType
If anyone knows better/cleaner approach, I'll be happy if you paste here some snippet.
@Dzoukr That looks ok, could you add it to the docs?
Closing this old issue
I was wondering how to properly generate Enum type from TP (erased)? The only resouce I was able to find is quite old response on StackOverflow. https://stackoverflow.com/questions/15174637/how-to-generate-enums-in-a-f-type-provider
Please, is there any clean example how to do it (and do it right way)?
Meanwhile @isaacabraham had an idea to have a look at SqlClient TP. https://github.com/fsprojects/FSharp.Data.SqlClient/blob/master/src/SqlClient/SqlEnumProvider.fs#L157-L167
I'll try to investigate and if it would work, I'll update this "issue" (it is more like ask for help then real issue) with some example.