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
ary
method is very useful. This adaptation is particularly valuable in contexts like employing theMemo
class or theUseAsyncState.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 ofA
type. -
Args1<A>
: represents a argument ofA
type. -
Args2<A, A2>
: represents two arguments ofA
,A2
type consecutively. -
Args3<A, A2, A3>
: represents three arguments ofA
,A2
,A3
type consecutively. -
ArgsX2<A>
: represents two arguments ofA
type. -
ArgsX3<A>
: represents three arguments ofA
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 argumentsT
type.
Usage
To use it, you can create an instance and provide the specified types of arguments, e.g.:
Alternatively, you can access the arg1
, arg2
, arg3
properties to retrieve individual arguments, like so:
The ArgsX2
and ArgsX3
classes are similar to the Args2
and Args3
classes, but these classes are set to the same type of arguments.
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.:
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.:
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.:
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.: