> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dairy.foundation/llms.txt
> Use this file to discover all available pages before exploring further.

# Subsystems

Mercurial provides a `Subsystem` interface that is a thin wrapper over `Feature`
from Core, you should go read the documentation on `Feature` if you wish to use
the `Subsystem` interface.

The `Subsystem` interface is entirely optional, and you may implement subsystems
however you wish, but we'll take a quick look at what they provide on top of
`Feature`.

However you decided to implement subsystems, they are highly recommended as a
great way to separate parts of your code from each other, and allow them to be
recombined on demand. Dairy Core's `Feature` system is a great way to do
subsystems.

We'll use these example subsystems:
[Java](https://github.com/Dairy-Foundation/Dairy/blob/master/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/examples/mercurial/JavaSubsystem.java)
[Kotlin](https://github.com/Dairy-Foundation/Dairy/blob/master/TeamCode/src/main/kotlin/org/firstinspires/ftc/teamcode/examples/mercurial/KotlinSubsystem.kt)

<CodeGroup>
  ```java Java theme={null}
  // these methods delegate to Mercurial.INSTANCE
  JavaSubsystem.INSTANCE.getDefaultCommand();
  JavaSubsystem.INSTANCE.setDefaultCommand(new Lambda("default"));
  // the default command needs to be reset every OpMode, so as per the example,
  // add it to the preInit hook.

  // the subsystemCell function is for use inside the class, it generates a
  // SubsystemObjectCell for the Subsystem. See the next section for more.
  JavaSubsystem.INSTANCE.subsystemCell(() ->
  	hardwareMap.get(DcMotorEx.class, "m");
  );

  // this static constant is a good starting point for the dependency of your
  // Subsystem
  Subsystem.DEFAULT_DEPENDENCY
  ```

  ```kt Kotlin theme={null}
  // these methods delegate to Mercurial
  KotlinSubsystem.defaultCommand
  KotlinSubsystem.defaultCommand =  Lambda("default")
  // the default command needs to be reset every OpMode, so as per the example,
  // add it to the preInit hook.

  // the subsystemCell function is for use inside the class, it generates a
  // SubsystemObjectCell for the Subsystem. See the next section for more.
  KotlinSubsystem.subsystemCell {
  	hardwareMap.get(DcMotorEx::class.java, "m")
  }

  // this static constant is a good starting point for the dependency of your
  // Subsystem
  Subsystem.DEFAULT_DEPENDENCY
  ```
</CodeGroup>

Take a look at the examples above, and read more about Core's `Feature`s in
order to learn more about how to use Subsystems.

# SubsystemObjectCell

`SubsystemObjectCell` is like Core's `OpModeLazyCell` or `OpModeFreshCell` that
is tied to a particular `Feature` being attached, and `Mercurial` being
attached.

It ensures that its contents have been evaluated post init, and will ensure that
it is empty after the OpMode finishes.

This makes it perfect for OpMode lifetime scoped objects with Mercurial
Subsystems.

Take a look at both [OpModeLazyCell and
OpModeFreshCell](/Core/templating/using_opmode_lazy_cell) in Core.
