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

discriminated union boolean tag inference fails for the 'false' case

EDIT: The problem was simply not using strict mode, which correctly disallowed the types with null properties and correctly inferred both true and false cases. I'm writing a discriminated union where the tag is a boolean, but trying to use !x.tag as…
Ran Lottem
  • 416
  • 5
  • 14
0
votes
1 answer

Is there the equivalent of Python 3's Optional for generics in C#?

I want to have a List of a type Regex. But the list can have None (or null). So I'd like the syntax List or something similar. There is a blog post here that has the same concept. But I'm hoping there may be something baked into C# I…
JGFMK
  • 7,107
  • 4
  • 46
  • 80
0
votes
1 answer

TypeScript discriminated union filtering fails on discriminated key

I've got the following code that I'm using to track async request statuses. It uses _type as a discriminator, as well as status. In the following code I define two AsyncStatus types: LoginAsyncStatus and SearchAsyncStatus. They differ by _type and…
Devin
  • 2,025
  • 2
  • 20
  • 25
0
votes
0 answers

How to use a literal type value as the discriminant of a discriminated union?

I'd like to use rex-tils Enum (somewhat similar to a String literal type), as the discrimant of a discriminated union. In other words, considering the following (simplified) example, I'd like to let the Animal type know that name should be of…
Dirty Henry
  • 6,978
  • 6
  • 48
  • 92
0
votes
0 answers

Can not implement discriminated union and serialize it

Hello i am trying to create a hierarchy of classes using a discriminated unionand it seems i can't serialize them.I keep getting this error : Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'AsRun' with type…
Bercovici Adrian
  • 6,415
  • 9
  • 47
  • 90
0
votes
2 answers

Pulling out one discriminated union case in F#?

Suppose I have a DU like: type Fruit = | Apple of string * bool | Banana of string | Cherry of string Then I have a collection like this: fruits : Fruit list I want to pull out all of the Apple instances to perform some computation: // apples…
sdgfsdh
  • 24,047
  • 15
  • 89
  • 182
0
votes
0 answers

Generic type level scheme in typescript

I want to create a tagged scheme of input and output types for function, so that having a tag passed into a function it would infer the input and output types. Here is an example: type Aa = { tag: 'a', a: 2 } type Ab = { tag: 'b', a:…
0
votes
6 answers

Collection of Union types scala

Is it possible in scala to have a collection of a union types. There are a few approaches to union types discussed here The top rated answer feels the most native, i have something like this: sealed trait StringOrNumber[T] object StringOrNumber { …
0
votes
1 answer

Difference between union and quantified union in B?

I am currently using Atelier B and would like to have a better understanding of the difference between union and quantified union. An example would be much appreciated
0
votes
1 answer

Strange union type behavior in TypeScript

Could anyone explain the following behavior in TypeScript (2.4.1)? Scenario: I have a button which could be "red" or "red and round" (modifications). I would like to have the following syntax to describe it: button.mods = "red"; button.mods =…
dennis
  • 442
  • 2
  • 17
0
votes
1 answer

XML Schema for Tagged Union

I have an XML doc that includes a repeating series of addresses whose actual content depends on the value of an included enumeration. I suppose this is a type of "tagged union" [or "discriminated union"?]: {
badfd
  • 63
  • 7
0
votes
1 answer

Can one have a polymorphic factory for types inheriting from an interface?

Following advice in SO and other places I tried to implement polymorphism using interfaces, as in the following simplified example. type IFace = // abstract methods abstract member foo: int -> int type X(i: int) = // some code …
Soldalma
  • 4,183
  • 2
  • 18
  • 34
0
votes
1 answer

Idiomatic Typescript Enum Discriminated Union

As of typescript 2.0 you can use discriminated unions with an enum as the discriminant like this: export function getInstance(code: Enum.Type1, someParam: OtherType1): MyReturnType1; export function getInstance(code: Enum.Type2, someParam:…
chris
  • 5,365
  • 5
  • 38
  • 52
0
votes
1 answer

Modelling world in terms of "or" and "and"

Thomas user said in the comment: I think modelling the world in terms of "or" (aka discriminated union) and "and" (aka records) is a very powerful functional trick. Sometimes, functions/interfaces are useful too, but if I tend to stick to…
MiP
  • 4,336
  • 2
  • 19
  • 36
0
votes
1 answer

Using prior cases in case type signature

I know that discriminated unions can refer to themselves, e.g. type Tree= | Node of Tree | Leaf but is there any means to refer to other cases in the type signatures? Both of the following raise errors that "The type 'Year' is not defined"…
Ghillie Dhu
  • 203
  • 2
  • 9
1 2 3
24
25