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

372 questions
100
votes
15 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,007
  • 7
  • 41
  • 59
50
votes
3 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,406
  • 3
  • 23
  • 20
36
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,705
  • 2
  • 17
  • 31
32
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,544
  • 14
  • 25
25
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
  • 14,317
  • 2
  • 38
  • 52
24
votes
2 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
  • 7,200
  • 5
  • 42
  • 78
24
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,200
  • 2
  • 11
  • 12
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
  • 14
23
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
  • 20,962
  • 21
  • 90
  • 152
22
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
  • 36,792
  • 11
  • 94
  • 126
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…
17
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
  • 90,453
  • 117
  • 313
  • 521
15
votes
3 answers

Enum vs non-member discriminated union

I've just noticed that there's only a little difference in declaring a non-member discriminated union: type Color = | Red | Green | Blue and declaring an enum: type Color = | Red = 0 | Green = 1 | Blue = 2 What are their…
pad
  • 39,834
  • 7
  • 83
  • 159
14
votes
1 answer

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
  • 9,662
  • 6
  • 65
  • 76
14
votes
1 answer

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
  • 401
  • 1
  • 4
  • 10
1
2 3
24 25