Skip to content

Args

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 ary method is very useful. This adaptation is particularly valuable in contexts like employing the Memo class or the UseAsyncState.withArg hook, 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 of A type.
  • Args1<A> : represents a argument of A type.
  • Args2<A, A2> : represents two arguments of A , A2 type consecutively.
  • Args3<A, A2, A3> : represents three arguments of A , A2 , A3 type consecutively.
  • ArgsX2<A> : represents two arguments of A type.
  • ArgsX3<A> : represents three arguments of A type.

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 , ArgsX3 only): A property to get the second argument.
  • arg3( Args3 , ArgsX3 only): A property to get the third argument.
  • toList() : A method to get the list of arguments T type.

Usage

To use it, you can create an instance and provide the specified types of arguments, e.g.:

1
void myFunction(Args2<int, String> args) {
2
print(args.arguments); // [1, "test"]
3
}
4
5
myFunction(Args2(1, "test"));

Alternatively, you can access the arg1, arg2, arg3 properties to retrieve individual arguments, like so:

1
void myFunction(Args3<int, String, bool> args) {
2
print(args.arg1); // 1
3
print(args.arg2); // "test"
4
print(args.arg3); // true
5
}
6
7
myFunction(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.

1
void myFunction(ArgsX3<int> args) {
2
// get the list of arguments
3
print(args.arguments); // [1, 2, 3]
4
// or get the individual arguments
5
print(args.arg1); // 1
6
print(args.arg2); // 2
7
print(args.arg3); // 3
8
}
9
10
myFunction(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.:

1
void myFunction(Args2<int, String> args) {
2
print(args.arg1); // 1
3
print(args.arg2); // "hello"
4
}
5
6
myFunction(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.:

1
void myFunction(ArgsX2<int> args) {
2
print(args.arg1); // 1
3
print(args.arg2); // 2
4
}
5
6
// these are valid
7
myFunction(Args2(1, 2));
8
myFunction(Args3(1, 2, 3));
9
myFunction(ArgsX3(1, 2, 3));
10
11
// these are invalid
12
myFunction(Args([1, 2]));
13
myFunction(Args1(1));
14
myFunction(Args2(1, "foo"));
15
myFunction(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 equal
2
print(Args1(1) == Args1(1)); // true
3
print(Args2(2, "foo") == Args2(2, "foo")); // true
4
print(ArgsX3(3, "bar", false) == ArgsX3(3, "bar", false)); // true
5
print(Args([1, "foo"]) == Args2(1, "foo")); // true
6
print(Args2(2, "bar") == ArgsX2(2, "bar")); // true
7
print(ArgsX3(3, "foo", false) == Args([3, "foo", false])); // true
8
9
// The argument does not match
10
print(Args1(1) == Args1(2)); // false
11
// The argument does not match because Map object has a different reference
12
print(Args1({"foo": "bar"}) == Args1({"foo": "bar"})); // false
13
// Different number of arguments
14
print(Args([1, 2]) == Args1([1, 2])); // false
15
print(Args2(2, "foo") == Args3(2, "foo", false)); // false
16
print(Args3(1, "foo", false) == Args([1, "foo", false, "bar"])); // false
17
// Different type of arguments
18
print(ArgsX3(3, "bar", false) == Args3("bar", 3, false)); // false

Ary 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.:

1
import 'package:reactter/reactter.dart';
2
3
// The `ary` extension is available for all functions with positional arguments
4
int addNum(int num1, int num2) => num1 + num2;
5
final addNumAry = myFunction.ary; // int Function(Args2(int, int))
6
addNumAry(Arg2(1, 1)); // 2
7
8
// Alternatively, it can be called directly
9
addNum.ary(ArgX2(2, 2)); // 4
10
11
// Any function with any number of arguments can be converted
12
void foo(int arg1, String arg2, bool arg3, List<int> arg4) => print("$arg1, $arg2, $arg3, $arg4");
13
final fooAry = foo.ary; // void Function(Args<dynamic> args)
14
fooAry(Args([1, "test", true, [1, 2, 3]])); // 1, test, true, [1, 2, 3]