Skip to content
On this page

Options and variants


In SmartPy usage of the value None is reflected in the type: for example, an optional integer is of type sp.option[]. This type is inhabited by None and by sp.Some(n) for any value n of type

x.is_none() → sp.bool

Checks whether the given value is None. For example None.is_none() == True and sp.Some(42).is_none() == False

x.is_some() → sp.bool

Checks whether the given value is of the form sp.Some(...). For example None.is_some() == False and sp.Some(42).is_some() == True.

x.unwrap_some([error]) → t

Extracts the argument of an option value if it is of the form sp.Some(...). Raises an exception (that can be specified as error) if x == None.

For example, sp.Some(42).unwrap_some() == 42, whereas None.unwrap_some("oops") raises the exception "oops".


Variants in SmartPy are enumerations of cases, where each case comes with an extra value. In other languages similar features, such as enums, sum types, and tagged/disjoint unions, exist.

For example, sp.variant(, Rectangle=sp.pair[,]) is a variant type with two cases. Its values are sp.variant.Circle(r) (for any r of type and sp.variant.Rectangle(h, w) (for any h and w of type

x.is_variant.V() → sp.bool

Checks whether a value is of the given variant V, for example:

c = sp.variant.Circle(2)
assert c.is_variant.Circle()
x.unwrap.V([exception]) → t

Obtains the argument of a given variant V. Raises an error if it is of a different variant. For example:

c = sp.variant.Circle(2)
assert c.unwrap.Circle() == 2
r = c.unwrap.Rectangle()  # raises an exception