Intro to Cells + RefCell
Cells are Dairy’s shared interior mutability reference wrapper. This sounds complicated, but its not. Cells basically allow you to wrap some data with a common access interface.
The most basic cell you could use is the RefCell
. We’ll use Units
as our inner contents.
The contents of a Cell are often referred to as the ‘contents’ or the ‘ref’ (short for reference). We can access the contents like so:
and we can set the contents like so:
This is because all Cells implement Consumer and Supplier. This allows them to be used directly in many places across Dairy, as these two interfaces are used often.
Additionally, Cells keep track of when access occurred.
Its also worth noting that Cells can be used for delegation in Kotlin:
This means that distance
is of type Distance
, and when you get, or set it,
the behaviour is delegated to the millimeters RefCell.
RefCell is useful, but Cells can be extended to have a myriad of other behaviours, we’ll go over some of the more common and useful