Δ Delta
Read Online · Appendix B

Result<T> and Money<T> — Complete Implementations

From the free book Delta: Closing the Specification Gap (Dhuri, 2026). The PDF is the canonical edition — figures, tables, and code formatting are simplified online.

B Result<T> and Money<T> — Complete Implementations Copy these into your shared library before using any recipe from Part II.
B.1 C# — Result<T> and Result // src/Common/Result.cs namespace YourCompany.Common; public sealed class Result<T> { public bool IsSuccess{get;}
public T? Value{get;}
public string? ErrorCode{get;}
public string? ErrorMessage{get;}

private Result(bool ok,T? v,string? c,string? m) {IsSuccess=ok;Value=v;ErrorCode=c;ErrorMessage=m;} public static Result<T> Success(T v)=>new(true,v,null,null); public static Result<T> Failure(string c,string m)=>new(false, ,→ default,c,m); public Result<TOut> Map<TOut>(Func<T,TOut> f)=> IsSuccess?Result<TOut>.Success(f(Value!)):Result<TOut>. ,→ Failure(ErrorCode!,ErrorMessage!); public T GetValueOrThrow()=>IsSuccess?Value!:throw new ,→ InvalidOperationException( $"[{ErrorCode}] {ErrorMessage}"); } B

Result<T> and Money<T> — Complete Implementations

145

public sealed class Result { public bool IsSuccess{get;}
public string? ErrorCode{get;}
,→ public string? ErrorMessage{get;} private Result(bool ok,string? c,string? m){IsSuccess=ok; ,→ ErrorCode=c;ErrorMessage=m;} public static Result Success()=>new(true,null,null); public static Result Failure(string c,string m)=>new(false,c,m); }

B.2 C# — Money<T> // src/Domain/ValueObjects/Money.cs namespace YourCompany.Domain.ValueObjects; public struct USD{} public struct EUR{} public struct GBP{} public sealed record Money<T>(decimal Amount,int Scale=2) where T: ,→ struct { [Obsolete("Never use double for money",error:true)] public Money(double a):this((decimal)a){} public Money<T> Add(Money<T> o)=>this with{Amount=Amount+o.Amount ,→ }; public Money<T> Subtract(Money<T> o)=>this with{Amount=Amount-o. ,→ Amount}; public Money<T> Multiply(decimal f, MidpointRounding m=MidpointRounding.AwayFromZero)=> this with{Amount=Math.Round(Amount*f,4,m)}; public Money<T> Round(MidpointRounding m=MidpointRounding. ,→ AwayFromZero)=> this with{Amount=Math.Round(Amount,Scale,m)}; public bool IsPositive=>Amount>0m; public static Money<T> Zero=>new(0m); }

B.3 Java — Result<T> B
Result<T> and Money<T> — Complete Implementations

146

// com/yourcompany/common/Result.java package com.yourcompany.common; import java.util.*; import java.util.function.Function; public final class Result<T> { private final boolean s; private final T v; private final String code,msg; private Result(boolean s,T v,String c,String m){this.s=s;this.v=v; ,→ code=c;msg=m;} public static <T> Result<T> success(T v){ Objects.requireNonNull(v);return new Result<>(true,v,null, ,→ null);} public static <T> Result<T> failure(String c,String m){return new ,→

Result<>(false,null,c,m);}
public boolean isSuccess(){return s;} public Optional<T> getValue(){return Optional.ofNullable(v);} public String getErrorCode(){return code;} public String getErrorMessage(){return msg;} public <R> Result<R> map(Function<T,R> f){ return s?Result.success(f.apply(v)):Result.failure(code,msg);} ,→ public T getValueOrThrow(){ if(!s)throw new IllegalStateException("["+code+"] "+msg); return v;} }

B.4 Java — Money B

Result<T> and Money<T> — Complete Implementations

147

// com/yourcompany/domain/Money.java package com.yourcompany.domain; import java.math.*; import java.util.*; import java.util.Currency; public record Money(BigDecimal amount,Currency currency){ public Money{Objects.requireNonNull(amount);Objects. ,→ requireNonNull(currency);} public static Money of(String amount,String code){ return new Money(new BigDecimal(amount).setScale(4, ,→ RoundingMode.HALF_UP), Currency.getInstance(code));} public Money add(Money o){check(o);return new Money(amount.add(o. ,→ amount),currency);} public Money subtract(Money o){check(o);return new Money(amount. ,→ subtract(o.amount),currency);} public Money multiply(BigDecimal f){ return new Money(amount.multiply(f).setScale(4,RoundingMode. ,→ HALF_UP),currency);} public BigDecimal forOutput(){return amount.setScale(2, ,→ RoundingMode.HALF_UP);} private void check(Money o){if(!currency.equals(o.currency)) throw new ArithmeticException("Currency mismatch");} public static final Currency USD=Currency.getInstance("USD"); public static final Currency EUR=Currency.getInstance("EUR"); public static final Currency GBP=Currency.getInstance("GBP"); }

Cite this appendix
Dhuri, Sandeep. (2026). Delta: Closing the Specification Gap. Acuity Press. Appendix B.
DOI