discriminated-union

TypeScript: derive map from discriminated union

瘦欲@ 提交于 2019-11-30 15:27:47
I have a discriminated union type that differentiates types based on a string literal field. I would like to derive a mapped type that maps all of the types in the union to their corresponding discriminator literal values. e.g. export type Fetch = { type: 'fetch', dataType: string }; export type Fetched<T> = { type: 'fetched', value: T }; // union type discriminated on 'type' property export type Action = | Fetch | Fetched<Product>; // This produces a type 'fetch' | 'fetched' // from the type type Actions = Action['type']; // I want to produce a map type of the discriminator values to the

Is it possible to pass discriminated union tags as arguments?

♀尐吖头ヾ 提交于 2019-11-30 09:22:05
Is it possible to pass the type of a discriminated union tag to another function so it can use it for pattern matching? Non working example of what I mean: type Animal = Pig of string | Cow of string | Fish of string let animals = [Pig "Mike"; Pig "Sarah"; Fish "Eve"; Cow "Laura"; Pig "John"] let rec filterAnimals animalType animals = if animals = [] then [] else let rest = filterAnimals animalType (List.tail animals) match List.head animals with |animalType animal -> animal::rest // <- this doesn't work |_ -> rest printfn "%A" (filterAnimals Pig animals) Discriminated unions works best if

What is the Enum.GetName equivalent for F# union member?

三世轮回 提交于 2019-11-30 08:13:23
I want to get the equivalent of Enum.GetName for an F# discriminated union member. Calling ToString() gives me TypeName+MemberName, which isn't exactly what I want. I could substring it, of course, but is it safe? Or perhaps there's a better way? You need to use the classes in the Microsoft.FSharp.Reflection namespace so: open Microsoft.FSharp.Reflection ///Returns the case name of the object with union type 'ty. let GetUnionCaseName (x:'a) = match FSharpValue.GetUnionFields(x, typeof<'a>) with | case, _ -> case.Name ///Returns the case names of union type 'ty. let GetUnionCaseNames <'ty> () =

Narrowing a return type from a generic, discriminated union in TypeScript

戏子无情 提交于 2019-11-30 07:54:51
问题 I have a class method which accepts a single argument as a string and returns an object which has the matching type property. This method is used to narrow a discriminated union type down, and guarantees that the returned object will always be of the particular narrowed type which has the provided type discriminate value. I'm trying to provide a type signature for this method that will correctly narrow the type down from a generic param, but nothing I try narrows it down from the

Printing F# discriminated union

眉间皱痕 提交于 2019-11-30 04:04:49
问题 I am writing a F# program which parses a string into a AST type which is a discriminated union. When I use fsi (on Mono + Mac OS X) to run my code, the AST is printed out in a nice format. But when I use printfn "%s" <| ast.ToString() I get something like FSI_0002.Absyn+clazz . Writing a ToString method for all the discriminated union types would be a big chore. How do I make the value print the way fsi does it? 回答1: Have you tried printfn "%A" ast ? The %A specifier takes into consideration

Is it possible to pass discriminated union tags as arguments?

为君一笑 提交于 2019-11-29 14:24:25
问题 Is it possible to pass the type of a discriminated union tag to another function so it can use it for pattern matching? Non working example of what I mean: type Animal = Pig of string | Cow of string | Fish of string let animals = [Pig "Mike"; Pig "Sarah"; Fish "Eve"; Cow "Laura"; Pig "John"] let rec filterAnimals animalType animals = if animals = [] then [] else let rest = filterAnimals animalType (List.tail animals) match List.head animals with |animalType animal -> animal::rest // <- this

What is the Enum.GetName equivalent for F# union member?

自闭症网瘾萝莉.ら 提交于 2019-11-29 11:08:23
问题 I want to get the equivalent of Enum.GetName for an F# discriminated union member. Calling ToString() gives me TypeName+MemberName, which isn't exactly what I want. I could substring it, of course, but is it safe? Or perhaps there's a better way? 回答1: You need to use the classes in the Microsoft.FSharp.Reflection namespace so: open Microsoft.FSharp.Reflection ///Returns the case name of the object with union type 'ty. let GetUnionCaseName (x:'a) = match FSharpValue.GetUnionFields(x, typeof<'a

Scrap Your Boilerplate in f#

邮差的信 提交于 2019-11-28 20:49:29
I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming language? Not that I know of; without support built-in to the language/compiler, I expect the only alternative is a reflection-based version. (I don't know how Uniplate is implemented - do you?) Here's the code for a reflection-based version based on the example from the original presentation. I have not thought deeply about its limitations, but this

Questions regarding C++ non-POD unions

久未见 提交于 2019-11-28 11:01:18
C++11 gave us to possibility to use non-POD types within unions, say I have the following piece of code; union { T one; V two; } uny; Somewhere within my class, only one member will be active at a time, now my questions are rather simple. What is the default value of uny? - undefined? Whenever my class is destructed, which members (within the union), if any will be destructed? Suppose I have to std::typeinfo to keep track of which is the active member, should I then call the destructor explicitly on that member in the destructor? Does anyone have a link to the language proposal, which changed

Why is None represented as null?

廉价感情. 提交于 2019-11-28 09:56:27
CompilationRepresentationFlags.UseNullAsTrueValue can be used to Permit the use of null as a representation for nullary discriminators in a discriminated union Option.None is the most prominent example of this. Why is this useful? How is a null check better than the traditional mechanism for checking union cases (the generated Tag property)? It leads to perhaps unexpected behavior: Some(1).ToString() //"Some(1)" None.ToString() //NullReferenceException EDIT I tested Jack's assertion that comparing to null instead of a static readonly field is faster. [<CompilationRepresentation