Unit
The sp.unit
type in SmartPy represents a unique value, often used to signify the absence of a meaningful result or value. It corresponds to the unit
type in Michelson.
Example: Contract parameters
On Tezos every contract has a parameter type. For example, a contract of type sp.contract[sp.int]
can be called with an argument of type sp.int
. To do this from within any entrypoint one would write (assuming c
has type sp.contract[sp.int]
):
sp.transfer(42, sp.mutez(0), c)
But what if a contract doesn't require any arguments? In this case it can simply specify an expected parameter of type sp.unit
, giving the contract the type sp.contract[sp.unit]
.
In order to call such a contract from an entrypoint, one would then use the unit value ()
(assuming c
has type sp.contract[sp.unit]
):
sp.transfer((), sp.mutez(0), c)
See also: sp.transfer, contracts
Example: Variants
SmartPy variants can be used to represent alternative cases with extra information, e.g.:
shape : type = sp.variant(
Circle=sp.int,
Rectangle=sp.pair[sp.int, sp.int]
)
As you can see, a circle comes with only one number (its radius) whereas a rectangle carries two numbers (length and width).
But what about cases that don't require any extra info? sp.unit
to the rescue! Here is a type that would model events coming from a physical sensor:
senser_event : type = sp.variant(
TemperatureChange=sp.int,
HumidityChange=sp.int,
MotionDetected=sp.unit
)
Temperature and humidity changes each come with a number, whereas for detected motion there is no extra information, hence sp.unit
.