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

swap non-active std::unique_ptr data members for union

Given a union: #include #include #include #include #include #include struct A { int a; }; struct B { int b; }; template< typename X > struct S { std::size_t tag; …
Tomilov Anatoliy
  • 13,614
  • 8
  • 46
  • 134
0
votes
3 answers

Is it possible to automatically extract type from discriminated union?

Is it possible to extract the type of a discriminated union to initialize an "auto" variable? It's easy enough is you pass the type to a template, but I'd like something "auto". A solution using a visitor function or using a bounded list of types…
KentH
  • 1,152
  • 1
  • 12
  • 22
0
votes
1 answer

Copying a discriminated union object

I want to make a copy of an object that is of a discriminated union type, with one or two particular fields assigned different values and any other fields copied straight across. The tricky part is that I'm trying to write a function to do this,…
rwallace
  • 26,045
  • 30
  • 102
  • 195
0
votes
1 answer

F# Using Discrimated Union Types

I want to return a discriminated union type from a function - it conflicts with type inference - how should I change my code so that getKeyA returning KeyA gets changed into Key? type KeyA = {keyString:string} type KeyB = {keyInt:int} type…
weismat
  • 6,743
  • 3
  • 38
  • 55
0
votes
1 answer

F# Discriminated Unions access values

I have a application which runs as a server, dose some calculations and returns a value. I have created a discriminated union type of MessageType so I can have different types of messages passed between applications. The MessageType is made up of an…
Alan Mulligan
  • 937
  • 2
  • 13
  • 32
0
votes
1 answer

F#: Using object expression with discriminated union

I have a recursive function that contains a series of matches that either make the recursive call back to the function, or make a call to failwith. This is basically a hybrid implementation of the recursive descent parser descibed in Don Syme's…
Joe
  • 664
  • 4
  • 18
0
votes
1 answer

Can the F# compiler use currying to separate code paths based on type?

Can the F# compiler separate out code paths by currying a function in which different types imply different paths through subsequent called functions? Consider the following discriminated union. There are 2 possibilities, which are theoretically…
0
votes
1 answer

Can GetUnionCases be replaced with a generic in fsharp?

I have the following function. It is used to turn parts of a string into a Discriminated union case. I had specific ones and have been deduping and generalising the code. let extractor ty (characters:list)= //find the UnionCaseInfo for the…
CodeBeard
  • 515
  • 3
  • 10
0
votes
1 answer

How to use a member of an F# discriminated union from a record member

I'm trying to make a record with discriminated unions as members, but after making the "API" record, I only have access to the unions and not what I put in them.... type DefaultExpr = Expr Ref -> int -> unit> type Option1Expr =…
0
votes
2 answers

Consuming Discriminated Union types in variable bindings

I have made a custom type and would like to create 2 variables that prove my type works as expected. type number = A of int | B of float;; let a = 0;; let b = 0.0;; How should I change the variable declarations to force them to type number?…
jth41
  • 3,542
  • 8
  • 50
  • 105
0
votes
3 answers

How add setter to to discriminated unions in F#

I want add setter property to discriminated unions, how I should to do it? f.e.: type Factor = | Value of Object | Range of String let mutable myProperty = 123 member this.MyProperty with get() = myProperty …
nhusnullin
  • 51
  • 1
  • 1
  • 3
-1
votes
1 answer

Determine type of property from other property

Inside of a class, I know that a property will be one of a discriminated union based on another property, but I can't figure out how to get TypeScript to reflect that. I've tried extending a discriminated union such as the following, but that…
1 2 3
24
25