A minimal, zero-dependency Result type implementation for Dart with standalone Right/Left constructors similar to dartz.
- 🚀 Simple API with
Right()andLeft()constructors - 🛡️ Type-safe error handling
- 🔄 Functional operations (
map,flatMap,fold) - 📦 Zero dependencies
- 🧪 Full test coverage
- 💯 Null safety
Add to your pubspec.yaml:
dependencies:
rsl: ^1.0.0import 'package:rsl/rsl.dart';
Result<String, int> parseNumber(String input) {
try {
return Right(int.parse(input));
} catch (e) {
return Left('Invalid number: $input');
}
}
void main() {
final result = parseNumber('42');
// Pattern matching
result.fold(
(error) => print('Error: $error'),
(number) => print('Success: $number'),
);
// Get value with fallback
final value = result.getOrElse(() => 0);
print(value); // 42
}Right(value)- Success resultLeft(error)- Failure result
isRight/isLeft- Check result typeright/left- Get value (throws if wrong type)fold(ifLeft, ifRight)- Pattern matchingmap(f)- Transform success valuemapLeft(f)- Transform failure valueflatMap(f)- Chain operations
getOrElse(orElse)- Get value or fallbackgetOrNull()- Get value or nullgetOrThrow()- Get value or throw errorswap()- Swap success/failure values
RSL provides a simpler alternative to dartz's Either type when you just need basic Result-type functionality:
// With dartz
Either<String, int> result = Right(42);
// With rsl
Result<String, int> result = Right(42);MIT - See LICENSE for details.