A late-fee workflow can make billing errors easy to express when every lifecycle stage shares one broad representation. Type safety limits what operations are allowed by carrying domain meaning in types, so invalid transitions can fail at compile time rather than relying only on checks inside methods. section 5.4.3 section 5.9
For example, a generic late-fee type can encode its lifecycle state: LateFee<Draft>, LateFee<Billed>, and LateFee<Rejected>. A behavior that accepts LateFee<Draft> communicates and enforces that it operates only on drafts, while a wider bound can deliberately accept any lifecycle state. section 5.4.3
Make an invalid billing path harder to write
Model lifecycle states as a sealed hierarchy and use a type variable for state. The data flow can then move from LateFee<Draft> through review into LateFee<Billed> or LateFee<Rejected>; attempting to use the wrong state where a draft is required does not compile. section 5.4.3 section 5.4.5
More types are not automatically better. Distinct types can expose invariants and improve correctness and understandability, but narrowly fitted representations can make ordinary aggregation or reuse awkward. Choose the constraints that matter, and use runtime defenses where flexibility matters more. section 3.4 section 5.4.3
Useful design questions include:
- Does this value carry business meaning that a plain String, int, or double would hide? section 3.4
- Should an invalid state be impossible to express, or is a runtime check sufficient? section 5.9
- Will separate state types preserve useful operations across related values, such as grouping totals? section 5.4.3
Listing 5.41 the current data model for our feature
public record PastDue(Invoice value) {}
public sealed interface Lifecycle {
record Draft() implements Lifecycle {}
record Billed(String invoiceId) implements Lifecycle {}
record Rejected(Reason reason) implements Lifecycle {}
record InReview(ApprovalId approvalId) implements Lifecycle {}
}
public record LateFee<State extends Lifecycle>(
String customerId,
USD total,
State state,
LocalDate invoiceDate,
LocalDate dueDate,
List<Invoice> includedInFee
){}
sealed interface ReviewedFee {
record Billable(LateFee<Draft > latefee)
implements ReviewedFee {}
record NeedsApproval(LateFee<Draft > latefee)
implements ReviewedFee {}
record NotBillable(LateFee<Draft> latefee, Reason reason)
implements ReviewedFee {}
}
List<PastDue> collectPastDue(List<Invoice> invoices) {...}
LateFee<Draft> buildDraft(List<PastDue> invoices) {...}
ReviewedFee assesDraft(LateFee<Draft> invoice) {...}
LateFee<? extends Lifecycle> submitBill(Billable draft){...}
LateFee<InReview> startApproval(NeedsApproval needsApproval) {...} Inspect a complete late-fee model that combines a sealed lifecycle with a generic state parameter and review-specific types. section 5.4.5
Type safety improves the odds rather than guaranteeing bug-free software: a type system can be bypassed. Its practical value is making intended states and transitions visible in APIs and shifting selected invariants from runtime conditions into compile-time checks. section 5.4.5 section 5.9










