Sum data type
Sum data type is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Data Structure
This illustrates a data structure, a means of storing data within a program.
- Task
Create a sum data type:
A sum data type is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time.
Sum data types are considered an algebraic data type and are also known as tagged union, variant, variant record, choice type, discriminated union, disjoint union or coproduct.
- Related task
- See also
- Array
- Associative array: Creation, Iteration
- Collections
- Compound data type
- Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
- Linked list
- Queue: Definition, Usage
- Set
- Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
- Stack
OCaml
<lang ocaml>type tree = Empty
| Leaf of int | Node of tree * tree
let t1 = Node (Leaf 1, Node (Leaf 2, Leaf 3))</lang>
Rust
<lang rust>enum IpAddr {
V4(u8, u8, u8, u8), V6(String),
}
let home = IpAddr::V4(127, 0, 0, 1);
let loopback = IpAddr::V6(String::from("::1"));</lang>