Args
Esta página aún no está disponible en tu idioma.
Args , also known as Generic Arguments, is a class designed to represent arguments of specified types.
It behaves as an immutable aggregate type like Record , albeit with a maximum of three arguments.
It is usefult to use in the following cases:
- Function Argument Bundling: When you need to pass multiple arguments to a function via a single argument.
- Argument Extraction: When you required to get the arguments of a function as a list.
- Adaptation for Single-Argument Expectation: In situations where a class or function anticipates a function with a single argument, but you need to provide it with a function that accepts multiple arguments, the
arymethod is very useful. This adaptation is particularly valuable in contexts like employing theMemoclass or theUseAsyncState.withArghook, where compatibility hinges on transforming a multi-argument function into a single-argument one.
Classes
Reactter provides these generic arguments classes:
-
Args<A>: represents one or more arguments ofAtype. -
Args1<A>: represents a argument ofAtype. -
Args2<A, A2>: represents two arguments ofA,A2type consecutively. -
Args3<A, A2, A3>: represents three arguments ofA,A2,A3type consecutively. -
ArgsX2<A>: represents two arguments ofAtype. -
ArgsX3<A>: represents three arguments ofAtype.
Properties & Methods
In each of the methods it provides these properties and methods:
arguments: A property to get the list of arguments.arg1: A property to get the first argument.arg2(Args2,Args3,ArgsX2,ArgsX3only): A property to get the second argument.arg3(Args3,ArgsX3only): A property to get the third argument.-
toList: A method to get the list of argumentsTtype.
Usage
Define Arguments
To use it, you can create an instance and provide the specified types of arguments, e.g.:
1void myFunction(Args2<int, String> args) {2 print(args.arguments); // [1, "test"]3}4
5myFunction(Args2(1, "test"));Alternatively, you can access the arg1, arg2, arg3 properties to retrieve individual arguments, like so:
1void myFunction(Args3<int, String, bool> args) {2 print(args.arg1); // 13 print(args.arg2); // "test"4 print(args.arg3); // true5}6
7myFunction(Args3(1, "test", true));The ArgsX2 and ArgsX3 classes are similar to the Args2 and Args3 classes, but these classes are set to the same type of arguments.
1void myFunction(ArgsX3<int> args) {2 // get the list of arguments3 print(args.arguments); // [1, 2, 3]4 // or get the individual arguments5 print(args.arg1); // 16 print(args.arg2); // 27 print(args.arg3); // 38}9
10myFunction(ArgsX3(1, 2, 3));Type compatibility
The Args classes are compatible with each other, so you can pass an Args instance to a function that expects an Args instance with fewer arguments, e.g.:
1void myFunction(Args2<int, String> args) {2 print(args.arg1); // 13 print(args.arg2); // "hello"4}5
6myFunction(Args3(1, "hello", false));The ArgsX2 and ArgsX3 classes are compatible with the Args2 and Args3 classes, respectively, so you can pass an ArgsX2 instance to a function that expects an Args2 instance with the same type arguments, e.g.:
1void myFunction(ArgsX2<int> args) {2 print(args.arg1); // 13 print(args.arg2); // 24}5
6// these are valid7myFunction(Args2(1, 2));8myFunction(Args3(1, 2, 3));9myFunction(ArgsX3(1, 2, 3));10
11// these are invalid12myFunction(Args([1, 2]));13myFunction(Args1(1));14myFunction(Args2(1, "foo"));15myFunction(Args3(1, "bar", false));Equality Comparation
The Args classes are comparable, so you can compare them with each other.
Two Args instances are considered equal if they contain the same number of arguments and the values in each corresponding position match, e.g.:
1// these are equal2print(Args1(1) == Args1(1)); // true3print(Args2(2, "foo") == Args2(2, "foo")); // true4print(ArgsX3(3, "bar", false) == ArgsX3(3, "bar", false)); // true5print(Args([1, "foo"]) == Args2(1, "foo")); // true6print(Args2(2, "bar") == ArgsX2(2, "bar")); // true7print(ArgsX3(3, "foo", false) == Args([3, "foo", false])); // true8
9// The argument does not match10print(Args1(1) == Args1(2)); // false11// The argument does not match because Map object has a different reference12print(Args1({"foo": "bar"}) == Args1({"foo": "bar"})); // false13// Different number of arguments14print(Args([1, 2]) == Args1([1, 2])); // false15print(Args2(2, "foo") == Args3(2, "foo", false)); // false16print(Args3(1, "foo", false) == Args([1, "foo", false, "bar"])); // false17// Different type of arguments18print(ArgsX3(3, "bar", false) == Args3("bar", 3, false)); // falseAry Function
The arity or adicity of a function is the number of arguments (i.e. inputs or parameters) it takes.
Use ary of Function extention to convert any Function with positional arguments to Function with generic argument, e.g.:
1import 'package:reactter/reactter.dart';2
3// The `ary` extension is available for all functions with positional arguments4int addNum(int num1, int num2) => num1 + num2;5final addNumAry = myFunction.ary; // int Function(Args2(int, int))6addNumAry(Arg2(1, 1)); // 27
8// Alternatively, it can be called directly9addNum.ary(ArgX2(2, 2)); // 410
11// Any function with any number of arguments can be converted12void foo(int arg1, String arg2, bool arg3, List<int> arg4) => print("$arg1, $arg2, $arg3, $arg4");13final fooAry = foo.ary; // void Function(Args<dynamic> args)14fooAry(Args([1, "test", true, [1, 2, 3]])); // 1, test, true, [1, 2, 3]