frx_annotation 1.0.0
frx_annotation: ^1.0.0 copied to clipboard
A code generation package for union types and pattern matching in pure Dart applications
frx_annotation #
A Dart package that provides code generation for union types and pattern matching in Dart applications.
Features #
- Generate pattern matching methods for union types in pure Dart
- Support for when, maybeWhen, and whenOrNull patterns
- Support for map, maybeMap, and mapOrNull patterns
- Type-safe union case handling
Getting Started #
Add the following dependencies to your pubspec.yaml:
dependencies:
frx_annotation: ^1.0.0
dev_dependencies:
build_runner: ^2.4.0
frx_generator: ^1.0.0
Usage #
- Create your union type with the
@frxannotation:
import 'package:frx_annotation/frx_annotation.dart';
part 'example.frx.g.dart';
@frx
sealed class Union {
const Union();
}
final class _First extends Union {
final String value;
const _First(this.value);
}
final class _Second extends Union {
const _Second();
}
final class _Third extends Union {
const _Third();
}
- Run the code generator:
dart run build_runner build
- Use the generated extension methods:
void example() {
final union = _First('hello');
// Using when pattern
final result = union.when(
first: (value) => 'First: $value',
second: () => 'Second',
third: () => 'Third',
);
// Using maybeWhen pattern
final maybeResult = union.maybeWhen(
first: (value) => 'First: $value',
orElse: () => 'Other case',
);
}
Additional Information #
License #
MIT License
Copyright (c) 2023 Your Name
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.