discriminated-union

How to enumerate a discriminated union in F#?

我怕爱的太早我们不能终老 提交于 2019-11-28 06:47:08
How can I enumerate through the possible "values" of a discriminated union in F#? I want to know if is there something like Enum.GetValues(Type) for discriminated unions, tough I am not sure over what kind of data I would enumerate. I would like to generate a list or array of a discriminated union with one item for each option. Yes, F# has it's own reflection layer build on top of .NET's reflection to help you make sense of types that are specific to F#, like discriminating unions. Here's the code that will let you enumerate a union's cases: open Microsoft.FSharp.Reflection type MyDU = | One |

Why is None represented as null?

旧街凉风 提交于 2019-11-27 03:17:00
问题 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

How can I duplicate the F# discriminated union type in C#?

拜拜、爱过 提交于 2019-11-27 02:42:46
问题 I've created a new class called Actor which processes messages passed to it. The problem I am running into is figuring out what is the most elegant way to pass related but different messages to the Actor. My first idea is to use inheritance but it seems so bloated but it is strongly types which is a definite requirement. Have any ideas? Example private abstract class QueueMessage { } private class ClearMessage : QueueMessage { public static readonly ClearMessage Instance = new ClearMessage();

Typescript Discriminated Union allows invalid state

喜你入骨 提交于 2019-11-26 17:56:07
I am trying to use a Typescript Discriminated Union to model a rather common scenario when loading data asynchronously: type LoadingState = { isLoading: true; } type SuccessState = { isLoading: false; isSuccess: true; } type ErrorState = { isLoading: false; isSuccess: false; errorMessage: string; } type State = LoadingState | SuccessState | ErrorState; According to my understanding, this should limit the allowed combinations of values according to the type definitions. However, the type system is happy to accept the following combination: const testState: State = { isLoading: true, isSuccess:

Discriminated union in C#

余生长醉 提交于 2019-11-26 15:45:29
[Note: This question had the original title " C (ish) style union in C# " but as Jeff's comment informed me, apparently this structure is called a 'discriminated union'] Excuse the verbosity of this question. There are a couple of similar sounding questions to mine already in SO but they seem to concentrate on the memory saving benefits of the union or using it for interop. Here is an example of such a question . My desire to have a union type thing is somewhat different. I am writing some code at the moment which generates objects that look a bit like this public class ValueWrapper { public

Typescript Discriminated Union allows invalid state

北城以北 提交于 2019-11-26 05:38:16
问题 I am trying to use a Typescript Discriminated Union to model a rather common scenario when loading data asynchronously: type LoadingState = { isLoading: true; } type SuccessState = { isLoading: false; isSuccess: true; } type ErrorState = { isLoading: false; isSuccess: false; errorMessage: string; } type State = LoadingState | SuccessState | ErrorState; According to my understanding, this should limit the allowed combinations of values according to the type definitions. However, the type

Discriminated union in C#

夙愿已清 提交于 2019-11-26 04:35:46
问题 [Note: This question had the original title \" C (ish) style union in C# \" but as Jeff\'s comment informed me, apparently this structure is called a \'discriminated union\'] Excuse the verbosity of this question. There are a couple of similar sounding questions to mine already in SO but they seem to concentrate on the memory saving benefits of the union or using it for interop. Here is an example of such a question. My desire to have a union type thing is somewhat different. I am writing