I've gotten the basics of type providers such as creating a type
let thisAssembly = Assembly.GetExecutingAssembly()
let t = ProvidedTypeDefinition(
thisAssembly,namespaceName,
xType.Attribute(xname "name").Value,
baseType = Some typeof<obj>)
but I need to be able to define record types and DU types is there any way to do this with type providers?
Discriminated unions are useful for heterogeneous data; data that can have special cases, including valid and error cases; data that varies in type from one instance to another; and as an alternative for small object hierarchies.
A discriminated union is a union data structure that holds various objects, with one of the objects identified directly by a discriminant. The discriminant is the first item to be serialized or deserialized. A discriminated union includes both a discriminant and a component.
In this case the new type is the “sum” of the integer type plus the boolean type. In F#, a sum type is called a “discriminated union” type. Each component type (called a union case) must be tagged with a label (called a case identifier or tag) so that they can be told apart (“discriminated”).
The concept of discriminated unions is how TypeScript differentiates between those objects and does so in a way that scales extremely well, even with larger sets of objects. As such, we had to create a new ANIMAL_TYPE property on both types that holds a single literal value we can use to check against.
Unfortunately, it's not possible to define any F# specific types like Discriminated Unions, Records, or Modules, in a type provider, only classes and namespaces, which is a bit unfortunate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With