rsl 1.0.0
rsl: ^1.0.0 copied to clipboard
A simple Result type implementation for Dart with standalone Right/Left constructors similar to dartz.
RSL - Result Simple Library #
A minimal, zero-dependency Result type implementation for Dart with standalone Right/Left constructors similar to dartz.
Features #
- ๐ Simple API with
Right()andLeft()constructors - ๐ก๏ธ Type-safe error handling
- ๐ Functional operations (
map,flatMap,fold) - ๐ฆ Zero dependencies
- ๐งช Full test coverage
- ๐ฏ Null safety
Installation #
Add to your pubspec.yaml:
dependencies:
rsl: ^1.0.0
Usage #
Basic Example #
import '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
}
API Reference #
Constructors
Right(value)- Success resultLeft(error)- Failure result
Methods
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
Extensions
getOrElse(orElse)- Get value or fallbackgetOrNull()- Get value or nullgetOrThrow()- Get value or throw errorswap()- Swap success/failure values
Comparison with dartz #
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);
License #
MIT - See LICENSE for details.