This document discusses domain typing in Ruby through the lens of functional programming and domain-driven design. It introduces concepts like defining types as sets of possible values, using types to improve safety and intellectual manageability, and modeling domains and behavior through state machines and explicit state transitions. While typing is not native to Ruby, libraries like dry-types, dry-struct, and dry-logic make it possible to integrate these approaches and reap their benefits, moving Ruby code in a more functional direction.
11. “How to shoot yourself in the foot using Perl
You shoot yourself in the foot, but
nobody can understand how you did it.
Six months later, neither can you.
1111
19. How it looks in F#
19
module CardGame =
type Suit = Club | Diamond | Spade | Heart
type Rank = Two | Three | Four | Five | Six | Seven | Eight
| Nine | Ten | Jack | Queen | King | Ace
type Card = Suit * Rank
type Hand = Card list
type Deck = Card list
type Player = {Name:string; Hand:Hand}
type Game = {Deck:Deck; Players: Player list}
type Deal = Deck -> (Deck * Card)
type PickupCard = (Hand * Card) -> Hand
27. Type is a set of possible values
type Suit = Club | Diamond | Spade | Heart
type Rank = Two | Three | Four | Five | Six | Seven | Eight | Nine
| Ten | Jack | Queen | King | Ace
type Card = Suit * Rank
27
40. “A contact must have at least one contact information”
Implies
▰email address only, or
▰postal address only, or
▰both email and postal addresses
40
52. Why is that cool?
Each state has own set of data
All states are explicitly documented
All transitions are explicitly documented
It’s safe because it makes you think about any
combination
52