Logo

dev-resources.site

for different kinds of informations.

Discriminated Unions

Published at
11/26/2023
Categories
100daystooffload
programming
types
datastructures
Author
stefanalfbo
Author
11 person written this
stefanalfbo
open
Discriminated Unions

Discriminated unions are a powerful data structure to use when modelling a domain in an application.

The name of this data type varies between programming languages, for instance:

Pattern matching and discriminated unions are great together and makes it really easy to express complex models in the code.

Here is a simple example to make a boolish type in F#:

type Answer = Yes | No

let response answer =
    match answer with
    | Yes -> "Correct answer"
    | No -> "Wrong answer"
Enter fullscreen mode Exit fullscreen mode

One popular example in OOP is to show inheritance with a Shape class. Here is an example but with discriminated unions instead:

type Shape =
    | Circle of float
    | Rectangle of float * float
    | Square of float

let calculateArea shape =
    match shape with
    | Circle(radius) -> Math.PI * radius * radius
    | Rectangle(width, height) -> width * height
    | Square(side) -> side * side
Enter fullscreen mode Exit fullscreen mode

In other words:

Use discriminated unions for concise and type-safe representation of complex data structures, promoting clarity, pattern matching, and compiler-enforced correctness.

Happy hacking!

types Article's
30 articles in total
Favicon
Matanuska ADR 009 - Type Awareness in The Compiler and Runtime
Favicon
Matanuska ADR 007 - Type Semantics for Primary Types
Favicon
Opkey Highlights Importance of Staying Informed About Testing Types for Enhanced Quality Assurance
Favicon
Understanding Next.js and TypeScript Tutorial
Favicon
Python Has Types, They Help
Favicon
YAGNI For Types
Favicon
TypeScript's Lack of Naming Types and Type Conversion in Angular
Favicon
Six Alternatives to Using any in TypeScript
Favicon
Some Types - Part 1
Favicon
Top 9 Essential Utility Types in TypeScript
Favicon
Introduction to TypeScript
Favicon
Error Types in JS
Favicon
Prefer strict types in Typescript
Favicon
Having a type system is more productive
Favicon
Javascript Data Types
Favicon
Simplifying Complex Type Display in TypeScript and VS Code
Favicon
Key Software Testing Types Every QA Needs to Know
Favicon
Understanding and Implementing Type Guards in TypeScript
Favicon
Choosing the Right Database: A Comprehensive Guide to Types and Selection Criteria
Favicon
TypeScript Template Literal Types: Practical Use-Cases for Improved Code Quality
Favicon
Language types for integration safety
Favicon
The cost of types
Favicon
Discriminated Unions
Favicon
Using variant types in ReScript to represent business logic
Favicon
Building React Components Using Unions in TypeScript
Favicon
How to Typescript to JSON with Butlermock
Favicon
Integration Testing Types: A Brief Guide
Favicon
The Benefits of Static Typing: A Developer's Perspective
Favicon
React - Uncaught TypeError: Cannot read properties of undefined (reading 'lat')
Favicon
Conjuring TypeScript's Magic with Mapped Types

Featured ones: