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
11
votes
0 answers

How do you provide a discriminated union from an F# type provider

Is it possible to emit a discriminated union type from an F# type provider? If so, is there any sample code available for doing so?
Daniel Bradley
  • 1,110
  • 12
  • 22
10
votes
2 answers

How to pattern match on union constructors in `for .. in`

In Haskell, if I have a list of union typed values like this: example :: [Either Int Char] example = [Left 3, Right 'b', Left 6, Left 9, Right 'c'] I can use a little "trick" to extract all the results matching some specific pattern: lefts ::…
Lynn
  • 8,876
  • 38
  • 66
10
votes
1 answer

Haskell's "deriving Show" in F#?

In Haskell it is easy to make an algebraic type/discriminated union "displayable" as a string by simply adding deriving Show to the type definition. In F# I end up writing things like: type Pos = | Pos of int * int override this.ToString()…
Miron Brezuleanu
  • 2,824
  • 2
  • 19
  • 31
9
votes
3 answers

Printing F# discriminated union

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…
Binil Thomas
  • 13,450
  • 10
  • 53
  • 68
9
votes
5 answers

Is it possible to pass discriminated union tags as arguments?

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…
monoceres
  • 4,356
  • 4
  • 28
  • 55
8
votes
4 answers

How to check the case of a discriminated union with FsUnit?

I'd like to check that a value is of a particular case of a discriminated union, without having to also check any included data. My motivation is to only test one thing with each unit test. An example is as follows (the last two lines give…
Mark Pattison
  • 2,695
  • 1
  • 20
  • 39
8
votes
3 answers

Type extension for discriminated union in F#

I have defined the following discriminated union: type Expr = | Con of Num | Var of Name | Add of Expr * Expr | Sub of Expr * Expr | Mult of Expr * Expr | Div of Expr * Expr | Pow of Expr * Expr Then I created a…
luksan
  • 7,305
  • 3
  • 32
  • 36
8
votes
3 answers

Adding constant fields to F# discriminated unions

Is it possible to add constant field values to F# discriminated unions? Can I do something like this? type Suit | Clubs("C") | Diamonds("D") | Hearts("H") | Spades("S") with override this.ToString() = // print out the letter…
Ralph
  • 29,142
  • 31
  • 124
  • 261
7
votes
2 answers

F# discriminated unions versus C# class hierarchies

I have the following code: public abstract class A ... public class B : A ... public class C : A ... void my_fct(A x) { if (x is B) { block_1 } else if (x is C) { block_2 } else { block_3 } } and I wonder if it is a good translation from…
Hugo
  • 535
  • 1
  • 3
  • 12
7
votes
3 answers

Type-safe discriminated unions in C#, or: How to limit the number of implementations of an interface?

First, sorry for the lengthy post. Basically, my question is this: I'm trying to reproduce the following F# discriminated union type in C#: type Relation = | LessThan of obj * obj | EqualTo of obj * obj | GreaterThan of obj * obj Can…
7
votes
0 answers

F# discriminated unions of user defined types; respecting custom attributes

I am trying to use a discriminated union to represent some user-defined types; however, the types that make up the DU cases contain custom attributes that are not being respected by the DU. For example, I am marshaling a file header that contains…
bieberman
  • 309
  • 3
  • 9
7
votes
1 answer

How do you get the Discriminated Union Type from a Case instance?

Given these two Discriminated Unions I'd like to get the DeclaringType from a case instance. type SingleCaseUnion = | One type MultiCaseUnion = | Two | Three An example for each case would be as follows: getDiscriminatedUnionType One =…
Daniel Little
  • 15,641
  • 10
  • 66
  • 89
7
votes
1 answer

F# generic constraint union type

I have been working with F# for a few months but did not find any satisfying solution for my problem. I would like to describe a succession of operations as a discriminated union of values, or operations on these values. In this way, my type Val<'o>…
B. J.
  • 71
  • 4
7
votes
1 answer

Serialization formats that support tagged unions

This question has been asked back in 2012 , but I'm looking for new updates. Are there any serialization formats that support tagged unions (aka sum types)? My requirements are that it has Java and .Net client libs, and it should be "reasonable…
Akash
  • 2,211
  • 1
  • 19
  • 35
7
votes
1 answer

F#: downcast a discriminated union

I have a discriminated union type: type F = | A of int | B of float Suppose I have a list of F that has been filtered to yield only objects of type A: let listOfAs=list.filter (fun f -> match f with | A(f') -> true | _ -> false) How can I work…
Robert Sim
  • 1,314
  • 8
  • 20
1 2
3
24 25