Lambda
Lambda
and StatefulLambda
are the primary way to write commands in
Mercurial.
These classes are immutable command builders, that allow you to compose a command programmatically, and are easy to read and use.
StatefulLambda
is the same as Lambda
, but has additional state stored in it,
that gets passed to each of its methods to manipulate.
Both Lambda
and StatefulLambda
start with sensible defaults:
- no requirements
- an empty init method
- an empty execute method
- instantly finishes
- an empty end method
- is interruptible
- allowed to be scheduled in OpModeState.ACTIVE only (start and loop)
Then, you can apply the builder methods to compose a new command, by adding to or replacing components of the current command.
Construction
Both Lambda
and StatefulLambda
are easy to construct:
Our StatefulLambda
will have a RefCell
with a String
in it as its state.
Go read the RefCell
documentation in Util for how it works. We need a cell or
similar in order to obtain interior mutability for a simpler piece of data like
a String
, but you could use a more complex class you’ve written yourself.
Building
There are easy methods to set parts of a Command:
Or add to the pre-existing ones, we’ll demonstrate some of the add methods, but there are equivalents for all the set methods.
Remember, each of these methods are non-mutating. This means than when you call them, you get back a new, different command. In the case of statefulLambda, they still share the same state reference, but other than that they are totally different.
This is a common theme across Dairy, and its not hard to deal with, but you must keep it in mind.
Finally, any command can be converted into a Lambda
or StatefulLambda
command.
This process is safe for Lambda.from(Command)
, it does not create a new object
if the argument is already a Lambda
or a StatefulLambda
.
But StatefulLamba.from(Command, STATE)
does not have this safety.
Lambda
and StatefulLambda
are clearly nice and easy ways to compose
commands! They are the recommended tool for writing commands in Mercurial.