Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing a Discriminated Union from an F# Type Provider

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?

like image 795
Rune FS Avatar asked Dec 06 '13 10:12

Rune FS


People also ask

Are the unions of f# Discriminated?

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”).

How is a discriminated union defined?

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.

When to use discriminated union?

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.

What is discriminated unions in TypeScript?

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.


1 Answers

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

like image 150
Gustavo Guerra Avatar answered Oct 04 '22 20:10

Gustavo Guerra