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