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)
Construction
BothLambda and StatefulLambda are easy to construct:
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:Lambda or StatefulLambda
command.
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.