LanguageExt Key Type Reference
Overview
Section titled “Overview”A quick reference guide for the core types of the LanguageExt library.
Fin - Result Type
Section titled “Fin - Result Type”Basic Usage
Section titled “Basic Usage”// Create successFin<int> success = 42;Fin<int> success2 = FinSucc(42);
// Create failureFin<int> failure = Error.New("An error occurred");Fin<int> failure2 = FinFail<int>(Error.New("Error"));
// Check resultif (result.IsSucc) { /* success */ }if (result.IsFail) { /* failure */ }Match - Pattern Matching
Section titled “Match - Pattern Matching”var message = result.Match( Succ: value => $"Success: {value}", Fail: error => $"Failure: {error.Message}");Map - Value Transformation
Section titled “Map - Value Transformation”Fin<int> number = 10;Fin<string> text = number.Map(n => n.ToString());// Result: "10"Bind - Chaining
Section titled “Bind - Chaining”Fin<int> Parse(string s) => int.TryParse(s, out var n) ? n : Error.New("Parse failed");
Fin<int> result = Fin<string>.Succ("42") .Bind(Parse) .Map(n => n * 2);// Result: 84IfFail - Default Value
Section titled “IfFail - Default Value”int value = result.IfFail(0);int value2 = result.IfFail(error => -1);Validation<Error, T> - Validation Type
Section titled “Validation<Error, T> - Validation Type”Basic Usage
Section titled “Basic Usage”// Create successValidation<Error, string> valid = Success<Error, string>("value");
// Create failureValidation<Error, string> invalid = Fail<Error, string>(Error.New("Error"));Apply - Parallel Validation
Section titled “Apply - Parallel Validation”var result = ( ValidateName(name), ValidateAge(age), ValidateEmail(email)).Apply((n, a, e) => new User(n, a, e));
// All validation errors are collectedSingle Field Validation
Section titled “Single Field Validation”Validation<Error, string> ValidateName(string name) => string.IsNullOrEmpty(name) ? Fail<Error, string>(Error.New("Name is required")) : Success<Error, string>(name);Error Collection
Section titled “Error Collection”result.Match( Succ: value => Console.WriteLine($"Success: {value}"), Fail: errors => { foreach (var error in errors) Console.WriteLine($"Error: {error.Message}"); });Option - Optional Value
Section titled “Option - Optional Value”Basic Usage
Section titled “Basic Usage”// When a value existsOption<int> some = Some(42);Option<int> some2 = 42; // Implicit conversion
// When no value existsOption<int> none = None;string message = option.Match( Some: value => $"Value: {value}", None: () => "No value");Map and Bind
Section titled “Map and Bind”Option<int> result = Some(10) .Map(n => n * 2) .Bind(n => n > 0 ? Some(n) : None);Default Value
Section titled “Default Value”int value = option.IfNone(0);int value2 = option.IfNone(() => GetDefaultValue());Either<L, R> - Binary Choice
Section titled “Either<L, R> - Binary Choice”Basic Usage
Section titled “Basic Usage”// Right (success value)Either<string, int> right = Right<string, int>(42);
// Left (error value)Either<string, int> left = Left<string, int>("Error");string result = either.Match( Right: value => $"Success: {value}", Left: error => $"Failure: {error}");Error Type
Section titled “Error Type”Creation
Section titled “Creation”// Basic errorvar error = Error.New("Error message");
// Code and messagevar error2 = Error.New("ERR001", "Error message");
// From exceptionvar error3 = Error.New(exception);
// With inner errorvar error4 = Error.New("Outer error", Error.New("Inner error"));Properties
Section titled “Properties”string code = error.Code;string message = error.Message;Option<Error> inner = error.Inner;Option<Exception> exception = error.Exception;Unit Type
Section titled “Unit Type”Purpose
Section titled “Purpose”// Result type for functions with no return valueFin<Unit> SaveToDatabase(Data data){ // Save logic return unit; // Success}
// Validation functionFin<Unit> ValidateNotEmpty(string value) => string.IsNullOrEmpty(value) ? Error.New("Value is empty") : unit;Prelude Static Methods
Section titled “Prelude Static Methods”Required Import
Section titled “Required Import”using static LanguageExt.Prelude;Frequently Used Methods
Section titled “Frequently Used Methods”// Option creationSome(42)None
// Either creationRight<L, R>(value)Left<L, R>(value)
// Validation creationSuccess<Error, T>(value)Fail<Error, T>(error)
// Fin creationFinSucc<T>(value)FinFail<T>(error)
// Unit valueunitLINQ Extensions
Section titled “LINQ Extensions”SelectMany (Bind)
Section titled “SelectMany (Bind)”var result = from x in Fin<int>.Succ(10) from y in Fin<int>.Succ(20) select x + y;// Result: 30Select (Map)
Section titled “Select (Map)”var result = from x in Some(10) select x * 2;// Result: Some(20)Where (Filter)
Section titled “Where (Filter)”var result = from x in Some(10) where x > 5 select x;// Result: Some(10)Collection Extensions
Section titled “Collection Extensions”// Immutable sequencevar seq = Seq(1, 2, 3, 4, 5);var head = seq.Head; // Some(1)var tail = seq.Tail; // Seq(2, 3, 4, 5)// Immutable arrayvar arr = Array(1, 2, 3);var added = arr.Add(4); // Returns a new arrayMap<K, V>
Section titled “Map<K, V>”// Immutable dictionaryvar map = Map(("a", 1), ("b", 2));var value = map.Find("a"); // Some(1)var updated = map.Add("c", 3);Commonly Used Patterns
Section titled “Commonly Used Patterns”Chaining Pattern
Section titled “Chaining Pattern”var result = GetUser(id) .Bind(ValidateUser) .Bind(UpdateUser) .Map(ToResponse);Parallel Validation Pattern
Section titled “Parallel Validation Pattern”Method 1: Tuple-based Apply (Recommended)
var result = ( ValidateField1(input.Field1), ValidateField2(input.Field2), ValidateField3(input.Field3)).Apply((f1, f2, f3) => new Output(f1, f2, f3));Method 2: fun-based Individual Apply
The fun function is a helper for lambda type inference that applies Apply step by step through currying.
// Wrap the constructor/factory with fun and call Apply individuallyvar result = fun((string f1, string f2, string f3) => new Output(f1, f2, f3)) .Map(f => Success<Error, Func<string, string, string, Output>>(f)) .Apply(ValidateField1(input.Field1)) .Apply(ValidateField2(input.Field2)) .Apply(ValidateField3(input.Field3));Or more concisely using Pure:
var result = Pure<Validation<Error>, Output>( fun((string f1, string f2, string f3) => new Output(f1, f2, f3))) .Apply(ValidateField1(input.Field1)) .Apply(ValidateField2(input.Field2)) .Apply(ValidateField3(input.Field3));A comparison of the two Apply approaches:
| Method | Characteristics | When to Use |
|---|---|---|
| Tuple Apply | Concise and intuitive | Recommended for most cases |
| fun Individual Apply | Currying-based, step-by-step application | Dynamic parameter count, advanced composition |
Option Chaining
Section titled “Option Chaining”var result = user .Map(u => u.Address) .Bind(a => a.City) .Map(c => c.Name) .IfNone("Unknown");Functorium Validation Helpers
Section titled “Functorium Validation Helpers”ValidationRules - Type-Safe Validation Entry Point
Section titled “ValidationRules - Type-Safe Validation Entry Point”ValidationRules<T> is a static class that starts a validation chain by specifying the value object type parameter once. The value object type name is automatically included in error codes.
using Functorium.Domains.ValueObjects.Validations.Typed;
// Chaining validation: NotNull → ThenNotEmpty → ThenMaxLengthValidationRules<Email>.NotNull(value) .ThenNotEmpty() .ThenMaxLength(255);
// Start methods: NotNull, NotEmpty, MinLength, MaxLength, ExactLength, etc.// Chaining methods: ThenNotEmpty, ThenMinLength, ThenMaxLength, ThenExactLength, ThenNormalize, etc.TypedValidation<TValueObject, T> - Type Information Wrapper
Section titled “TypedValidation<TValueObject, T> - Type Information Wrapper”The return type of ValidationRules<T>, which carries value object type information throughout the chain. It implicitly converts to Validation<Error, T>.
// TypedValidation implicitly converts to Validation<Error, T>TypedValidation<Email, string> typed = ValidationRules<Email>.NotNull(value);Validation<Error, string> validation = typed; // Implicit conversion
// Can be passed directly to CreateFromValidationpublic static Fin<Email> Create(string? value) => CreateFromValidation( ValidationRules<Email>.NotNull(value) .ThenNotEmpty() .ThenMaxLength(255), v => new Email(v));ValidationApplyExtensions - Tuple Apply Extensions
Section titled “ValidationApplyExtensions - Tuple Apply Extensions”ValidationApplyExtensions provides Apply overloads for Validation<Error, T> tuples, internally converting the K<Validation<Error>, T> returned by LanguageExt’s generic Apply with .As(). The caller does not need to call .As() directly.
using Functorium.Domains.ValueObjects.Validations;
// Returns concrete Validation<Error, R> without .As()var result = ( ValidateAmount(amount), ValidateCurrency(currency)).Apply((a, c) => new Money(a, c));// result type: Validation<Error, Money> (not K<>)
// Supports 2 to 5 tuplesNext Steps
Section titled “Next Steps”Check the framework type selection guide.