2 ms·
None of your examples use native C# pattern matching. And without language support like e.g. discriminated unions you can't have exhaustive pattern matching in
by fabian2k 1y ago
None of your examples use native C# pattern matching. And without language support like e.g. discriminated unions you can't have exhaustive pattern matching in C#. So you'll have to silence the warnings about the missing default case or always add one, which is annoying.
- CharlieDigital 1y agoI mean, it's not a stretch to see how you can use native pattern matching with ErrorOr result types. #!/usr/local/share/dotnet/dotnet run #:package ErrorOr@2.0.1 using ErrorOr; var computeRiskFactor = ErrorOr<decimal> () => 0.5m; // Just an example var applyAdjustments = ErrorOr<decimal> (decimal baseRiskFactor) => baseRiskFactor + 0.1m; // Just an example var approvalDecision = computeRiskFactor() .Then(applyAdjustments) .Match( riskFactor => riskFactor switch { < 0.5m => "Approved", < 0.75m and >= 0.5m => "Approved with Conditions", >= 0.75m and < 0.9m => "Manual Review", _ => "Declined" }, errors => "Error computing risk factor" ); Console.WriteLine($"Loan application: {approvalDecision}"); (Fully contained program, BTW) Here's the OCaml version: let compute_risk_factor () = 0.5 let apply_adjustments base_risk_factor = base_risk_factor +. 0.1 let approval_decision = let risk_factor = compute_risk_factor () |> apply_adjustments in match risk_factor with | r when r < 0.5 -> "Approved" | r when r < 0.75 -> "Approved with Conditions" | r when r < 0.9 -> "Manual Review" | _ -> "Declined" let () = print_endline approval_decision Still not functional enough?...Or you just don't like C#? No point moving goal posts.