Design Patterns in FP

In modern development, we constantly face the need to create reliable, maintainable, and extensible systems. Two technologies that help us achieve this are: Algebraic Data Types (ADTs) and Tagless Final.

Consider the evolution:

Stage 1: Programming Without Patterns

Hardcoded values, hard to test, impossible to extend.

def calculateTax(income: Double): Double =
  val database = new DatabaseConnection()
  val rate     = database.getTaxRate()
  income * rate

Stage 2: Algebraic Data Types

Clear data structure, type safety, pattern matching.

enum TaxResult:
  case Calculated(amount: Double)
  case Exempt(reason: String)
  case Error(message: String)

Stage 3: Tagless Final

Complete separation of business logic and implementation, easy testing.

trait TaxRepository[F[_]]:
  def getTaxRate: F[Double]

def calculateTax[F[_]: Monad](
    income: Double,
    repo: TaxRepository[F]
): F[TaxResult]

Summary

Algebraic data types give us:

Tagless Final adds:

ADTs and Tagless Final are not competitors, but complementary approaches:

// ADT for modeling data
enum PaymentError:
  case InsufficientFunds(balance: Double)
  case NetworkError(message: String)

// Tagless Final for structuring computations
trait PaymentService[F[_]]:
  def processPayment(amount: Double): F[Either[PaymentError, Unit]]

Links: