Questions tagged [discriminated-union]

Discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. They are also known as "sum types" in type theory. For [ocaml] use [variant] instead.

Discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types.

Discriminated unions are most important in functional languages such as and , where the compiler is able to verify that all cases of a disjoint union are always handled, avoiding many types of errors.

For a brief history of discriminated union, please visit its wikipedia page.

Related tags

506 questions
119
votes
17 answers

Discriminated union in C#

[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…
Chris Fewtrell
  • 7,555
  • 8
  • 45
  • 63
68
votes
4 answers

Kotlin and discriminated unions (sum types)

Does Kotlin have anything like discriminated unions (sum types)? What would be the idiomatic Kotlin translation of this (F#): type OrderMessage = | New of Id: int * Quantity: int | Cancel of Id: int let handleMessage msg = match msg…
ehnmark
  • 2,656
  • 3
  • 24
  • 20
43
votes
4 answers

How to enumerate a discriminated union in F#?

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…
michelpm
  • 1,795
  • 2
  • 18
  • 31
40
votes
1 answer

Constraining type in Typescript generic to be one of several types

I'm trying to constrain the input of a generic to be one of several types. The closest notation I've found is using union types. Here is a trivial example: interface IDict { // Error! An index signature…
bjnsn
  • 2,710
  • 2
  • 16
  • 14
39
votes
2 answers

TypeScript: Map union type to another union type

Is it possible to map a union type to another union type in TypeScript? What I'd Like to be able to do e.g. Given a union type A: type A = 'one' | 'two' | 'three'; I'd like to be able to map it to union type B: type B = { type: 'one' } | { type:…
bingles
  • 11,582
  • 10
  • 82
  • 93
34
votes
2 answers

Concise pattern match on single case discriminated union in F#

Say I have the following single case discriminated union: type OrderId = OrderId of string At some point I need the actual string. The way I've found for extracting it is: let id = match orderId with OrderId x -> x Is there a more concise way of…
Geoff
  • 1,634
  • 16
  • 25
32
votes
5 answers

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

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…
jayphelps
  • 15,276
  • 3
  • 41
  • 54
30
votes
3 answers

Discriminated Union of Generic type

I'd like to be able to use union discrimination with a generic. However, it doesn't seem to be working: Example Code (view on typescript playground): interface Foo{ type: 'foo'; fooProp: string } interface Bar{ type: 'bar' barProp:…
NSjonas
  • 10,693
  • 9
  • 66
  • 92
26
votes
7 answers

Idiomatic way to represent sum type (Either a b) in Clojure

Edited. My question now is: what idiomatic Clojure constructs are usually used instead of sum types in statically types languages? Consensus so far: use protocols if behaviour can be unified, use tagged pairs/maps otherwise, put necessary asserts in…
sastanin
  • 40,473
  • 13
  • 103
  • 130
24
votes
4 answers

When to use a Discriminate Union vs Record Type in F#

I am trying to get the basics of F# clear before moving on to complex examples. The material I'm learning has introduced both Discriminate Unions and Record types. I have reviewed the material for both, but it is still unclear to me why we would use…
Chris Tarn
  • 629
  • 5
  • 15
24
votes
4 answers

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

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?
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
22
votes
2 answers

Union types in Java

I've been working with C# for a while and trying to get more familiar with Java. So I'm trying to migrate some of the basic patterns I use on daily basis in C# even only to understand the gap between JVM and dotnet and figure out how to deal with…
Michal Pawluk
  • 561
  • 1
  • 5
  • 11
21
votes
2 answers

Do algebraic datatypes in Haskell equal discriminated unions in F#?

I am learning Haskell and would like to know whether the constructs known in Haskell as algebraic datatypes are the same that discriminated unions in F# or there are some subtle differences between them. I would also appreciate much a good…
20
votes
2 answers

Use of the and keyword in F# in discriminated unions

I was faced today with the following DUs declarations: type Grammar = Definition list and Definition = Def of string * Expression and Range = | Char of char | Range of char * char Why would one use the keyword and instead of type,…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
20
votes
1 answer

Typescript Discriminated Union allows invalid state

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 = {…
Yakimych
  • 17,612
  • 7
  • 52
  • 69
1
2 3
33 34