T
can be used in a contract as long as the contract implements the trait T
.
We will use a new Countable
component as an example:
increment
on a disabled counter will not increment it.
But we don’t want to add this switch logic to the Countable
component itself.
Instead, we add the trait Switchable
as a dependency to the Countable
component.
ISwitchable
trait defined in chapter “Components How-To”:
Countable
component to depend on the ISwitchable
trait:
Countable
component must implement the ISwitchable
trait:
ISwitchable
trait in the contract.
We already implemented a Switchable
component that provides an implementation of the ISwitchable
trait.
By using the Switchable
component in a contract, we can embed the implementation of the ISwitchable
trait in the contract and resolve the dependency on the ISwitchable
trait.
ISwitchable
trait implementation from the Switchable
component inside the Countable
component by embedding the implementation in the contract.
However, suppose we would like to turn off the switch after each increment. There’s no set
function in the ISwitchable
trait, so we can’t do it directly.
But the Switchable
component implements the internal function _off
from the SwitchableInternalTrait
that set the switch to false
.
We can’t embed SwitchableInternalImpl
, but we can add switchable::HasComponent<TContractState>
as a dependency inside CountableImpl
.
We make the Countable
component depend on the Switchable
component.
This will allow to do switchable::ComponentState<TContractState>
-> TContractState
-> countable::ComponentState<TcontractState>
and access the internal functions of the Switchable
component inside the Countable
component:
CountableContract
contract remains the same as in the previous example, only the implementation of the Countable
component is different.